views:

105

answers:

2

For some reason, Xcode will not take input from a file, while Visual C++ will. When I run this program in xcode, the variables numberRows and numberCols stay 0 (they are initialized to 0 in the main function). When I run it in Visual C++ they become 30 and 30 (the top line of maze.txt is "30 30" without the quotes). Any ideas why this is happening?

void readIn(int &numberRows, int &numberCols, char maze[][100]){

ifstream inData;
inData.open("maze.txt");

if (!inData.is_open()) {
 cout << "Could not open file. Aborting...";
 return;
}

inData >> numberRows >> numberCols;
cout << numberRows << numberCols;

inData.close();

return;

}

+1  A: 

There is something else wrong.
Unfortunately it is hard to tell.

Try flushing the output to make sure you get the error message:

void readIn(int &numberRows, int &numberCols, char maze[][100])
{
    ifstream inData("maze.txt");

    if (!inData) // Check for all errors.
    {
         cerr << "Could not open file. Aborting..." << std::endl;
    }
    else
    {
         // Check that you got here.
         cerr << "File open correctly:" << std::endl;

         // inData >> numberRows >> numberCols;
         // cout << numberRows << numberCols;

         std::string word;
         while(inData >> word)
         {
             std::cout << "GOT:(" << word << ")\n";
         }

         if (!inData) // Check for all errors.
         {
             cerr << "Something went wrong" << std::endl;
         }
    }
}
Martin York
the file does open, the problem is not in the if statement, but rather with inData >> numberRows >> numberCols;
Josh
Unlikely. I think the problem is more likely to be the file was not opened correctly and because the error message is not flushed to the output you just have not noticed.
Martin York
if the console does not display "Could not open file. Aborting..." wouldnt that mean the file is open? after taking away the exclamation point that statement does display. And sorry, I dont know what flushing is. First year csci student...
Josh
Output to std::cout is buffered. This means it is stored in an internal data structure for efficiency. It will not be displayed on the output until the buffer is flushed. There are a couple of ways to flush the buffer std::endl is the easiest (or use std::cerr which is not buffered).
Martin York
When I run the code you gave it prints this: File open correctly:00Something went wrong. If, however, I add .is_open() to the end of !inData, it only outputs: File open correctly:00. Not sure if this means anything...is !inData the same as !inData.is_open()?
Josh
I changed the code. What does it print out. (Copy and paste it). Don't use is_open() there are other errors.
Martin York
File open correctly:GOT:(30)GOT:(30)Something went wrongAnd what conditions make inData true (vs. the condition of it being open for "inData.is_open()")?
Josh
I am stumped why it is not working.
Martin York
If any of the error flags on a stream are set. Then the >> operator will quietly do nothing (even if the correct data is available). is_open() tests to see if the file is open but does not indicate weather another error has been set. While testing the stream in a boolean context will test if there is any problem (including failure to open) and return a value true only if the next >> operator has the possibility of working.
Martin York
Note: Using >> operator to read an int when there is no integer available on the input (white space will be ignored but not text) the operator will fail and set an error bit on the stream to indicate the error.
Martin York
PPS. In my last example I was expecting the "Something went wrong" because the extra while loop reads past the end of stream. But your original code should get 30 30 into the integers. There is something else in your code that is not apparent in the examples as the above code works find for me on all platforms.
Martin York
are you using XCode 3.2? See my answer to this question for something that worked (though I have no idea why it works). And thanks for your help, by the way!
Josh
A: 

interesting, so I followed the following suggestion from this post http://forums.macrumors.com/showthread.php?t=796818:

Under Xcode 3.2 when creating a new project based on stdc++ project template the target build settings for Debug configuration adds preprocessor macros which are incompatible with gcc-4.2: _GLIBCXX_DEBUG=1 _GLIBXX_DEBUG_PEDANTIC=1

Destroy them if you want Debug/gcc-4.2 to execute correctly.

Does anybody know why this works?

Josh