Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > problem with ifstream

problem with ifstream
Thread Tools
Fresh-Faced Recruit
Join Date: Jan 2004
Status: Offline
Reply With Quote
Jan 28, 2004, 08:14 PM
 
I'm having a problem with ifstream in C++. I'm using Xcode. I don't get how to get a simple use of ifstream to work. I'm trying to use the code below:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char buffer[256];
ifstream myfile("testtext");
while (!myfile.eof() )
{
myfile.getline (buffer,100);
cout << buffer << endl;
}

return 0;
}



I don't see anything wrong with that code. I made a text file using Text Wrangler (testtext) and put it in the project's folder. When I build n go, it just outputs an infinite amount of blank space and keeps going until I hit the "stop task" button. I must be doing something wrong since it's doing that instead of showing the one line of text I wrote. Could someone help me out with this?
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jan 30, 2004, 11:48 PM
 
The problem is that you're using a relative path for your text file. Unless the current directory in your program's environment is the same one containing the text file, the program can't find it. By default, I think it's set to your project's build directory. You can change it under the Executables group.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Fresh-Faced Recruit
Join Date: Jan 2004
Status: Offline
Reply With Quote
Jan 31, 2004, 11:42 AM
 
Thanks!!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:54 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2