You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

C++ ifstream

I'm in an intro to programming class and we use VS '08. At home/on my Mac, I prefer to use Xcode. I can't ever seem to get any of my programs to read in data from a .txt file in Xcode, but it works perfectly in VS '08. It's got to be with the differences of files systems between OS'...

I'm putting my .txt file in Debug in the project folder. If you want to look at a sample of my code:


#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
/*-----Function Prototypes-----*/
void importData(double []);
void displayTemps(double []);
int main () {

const int arraySize = 92;
double rawTemp[arraySize];

importData(rawTemp);

displayTemps(rawTemp);
}
void importData(double rawTemp[]){
int temp;
int count = 0;

ifstream inFile;
inFile.open("pgm7.txt");
while (inFile >> temp) {
rawTemp[count] = temp;
count++;
}
}
void displayTemps(double rawTemp[]){

for (int count = 0; count <= 31; count++) {
cout << setw(5) << right << count << setw(5) << rawTemp[count] << endl;
}
}

Any suggestions?

Mac OS X (10.6.3)

Posted on Apr 14, 2010 6:32 PM

Reply
Question marked as Top-ranking reply

Posted on Apr 14, 2010 7:24 PM

Nope. Just a bug that Apple seems content to never fix.

Go to Project-Edit Active Target. Select Build. Get rid of the GLIBCXX_DEBUG=1 GLIBCXX DEBUGPEDANTIC=1 in the preprocessor macros. Rebuild.

!http://img541.imageshack.us/img541/2034/screenshot20100414at101.png!
6 replies

Apr 15, 2010 10:12 AM in response to etresoft

And that has been my my headache since I started using Xcode. I tried to find the directory it refers to by default so I could just use
inFile.open("pgm7.txt");
like I do in VS, but I couldn't. Then I tried to write the entire path from the root of the volume, but that wasn't working either.

So I added a
if (!inFile)...
statement, which let me know that it wasn't finding the file. I then executed looked at the path above which looked like "/Users/benszymanski/Dropbox..." so I went back and changed the beginning of the path to start at "/Users...".

It works.

Much thanks to Eric Meyers and etresoft, you have both saved me from many hours of frustration and VS '08.

Apr 15, 2010 8:32 PM in response to ericmeyers

I just changed it so it reads
inFile.open("pgm7.txt");
, dropped pgm7.txt in the Debug folder and had my settings exactly like yours but it still didn't work...

Kind of odd, especially if you said that you got it to open and pull data in.

Regardless, I still appreciate your advice, tips and tricks- I'm learning a lot about Xcode. It's a pretty awesome IDE, even though I haven't tapped .005% of what it can do.

C++ ifstream

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.