xcode 3.2.1 c++ fstream problem "NOT SAME PROBLEM IN EVERY OTHER POST!!!"

Discussion in 'Mac Programming' started by modernlifeislov, Apr 29, 2010.

  1. modernlifeislov macrumors newbie

    Joined:
    Apr 29, 2010
    #1
    Okay so I have already taken off the macroguard and done everything all of the other forums have said to do but my if statement if (!indata) will print "cout" in the terminal regardless if the condition is met or not.

    My program runs fine, it outputs to the file okay. It's the problem with the terminal I'm concerned with.

    he is my code; I have highlighted the code in question:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    
    const int BINARY = 2;
    const int HEXADECIMAL = 16;
    
    char DigitToChar (int digit);
    // convert digit 0 to 16 to character
    void ConvertNumber (int base, int number, ostream& outdata);
    // convert decimal number to binary or hexadecimal
    // writes result to outdata
    int main()
    {
    	ifstream indata;
    	ofstream outdata;
    	
    	indata.open("data_input.txt");
    	outdata.open("data_output.txt");
    	int number;
    	[B]if (!indata)
    	{
    		outdata << "Missing Data Files!" << endl;
    		[COLOR="Red"]cout << "Missing Data Files!" << endl;[/COLOR]
    		system("PAUSE");
    		return EXIT_FAILURE;[/B]
    	}
    	indata >> number;
    	outdata << "Decimal          Binary          Hexadecimal" << endl;
    	while(indata)
    	{
    		outdata << setw(5) << number;
    		ConvertNumber(BINARY, number, outdata);
    		outdata << " ";
    		ConvertNumber(HEXADECIMAL, number, outdata);
    		outdata <<endl;
    		indata >> number;
    	}
    	indata.close();
    	outdata.close();
    	system("PAUSE");
    	return EXIT_SUCCESS;
    }
    char DigitToChar (int digit)
    {
    	static char HexList[] = "0123456789ABCDEF";
    	return HexList[digit];
    }
    void ConvertNumber (int base, int number, ostream& outdata)
    {
        int digit;
        char chdigit;
        static int count;
    	count = 0;
        
    	if (number == 0)
    		outdata << setw(12-count) <<" ";
    	if (number > 0)
    	{
    		count++;
    		digit = number%base;
    		chdigit = DigitToChar(digit);
    		ConvertNumber(base, number/base, outdata);
    		outdata << chdigit;
    	}
    }

    PLEASE HELP ME, THANK YOU
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    So you've run in the debugger, printed !infile at that line, and it's false, but one line of code in the if runs, but the others don't? Have you single-stepped through the code to see exactly what's being run?

    -Lee
     
  3. modernlifeislov thread starter macrumors newbie

    Joined:
    Apr 29, 2010
    #3
    It runs perfectly fine on Dev C++ in Windows. The problem is in xcode. I really really really don't want to go back to using parallels and windows, any help is appreciated
     
  4. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #4
    What exactly do you see happening?

    It seems to work ok for me. I didn't use XCode - I used g++ in Terminal. Are you sure you have a data_input.txt?
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Did you try what I said? Do you know what the workng directory that is being used is? Is your input file in that directory?

    -Lee
     
  6. modernlifeislov thread starter macrumors newbie

    Joined:
    Apr 29, 2010
    #6
    Here's everything that's going on

    [​IMG]

    it outputs to "data_output.txt" fine, but if you look in the terminal it still says missing data files.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    What do you expect this line to do? There is no PAUSE command on Mac OS X. This looks very Windows-specific.


    Since your pathnames are relative to the working directory, I recommend that you print the value of the working directory, and confirm it is correct for the pathname your code is using. If it's not, then you need to change something.

    You can get the working directory with the C function getcwd(). You can find its man page by searching Xcode's documentation for the function name.

    If you don't understand the significance of the working directory, read this:
    http://en.wikipedia.org/wiki/Working_directory
     
  8. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    Let's see if I have this correct. From your posted image it looks like you've started the Terminal which defaults you to your home directory which looks to be '/Users/bloodysundayhc'. Then you attempt to execute your program by specifiy a "complete" path-name pair and it fails.

    Try it in this order, change your working directory to the location of your executable first with:

    Code:
    cd /Users/bloodysundayhc/Desktop/Program\ 4\ CSC\ 210/build/Debug
    Now that your current working directory is correct execute the program with:

    Code:
    ./Program\ 4\ CSC\ 210
     

Share This Page