954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cannot read a file

Hello,
I cannot read a file, config.cfg, in the current directory on MacOS X 10.6.
I'm running a console app.

Can anybody tell me what I'm doing wrong? (By the way the getline() function returns no string.)

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char **argv) {
	std::string xpstr;
	std::ifstream myReadFile;
	myReadFile.open("./config.cfg");
		getline(myReadFile, xpstr, '\n');
		std::cout << xpstr.c_str();
	myReadFile.close();
	return 0;
}
epicbeast9022
Newbie Poster
15 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

I'm no mac user, but is the does the cfg file contain PLAIN text?

Also if that file is in the SAME directory

myReadFile.open("./config.cfg"); needs to be myReadFile.open("config.cfg");

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I'm no mac user, but is the does the cfg file contain PLAIN text?

Also if that file is in the SAME directory

myReadFile.open("./config.cfg"); needs to be myReadFile.open("config.cfg");

Yes it is, and I tried what you said previously - it still didn't work.
The cfg file is a plain text file.

epicbeast9022
Newbie Poster
15 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

What did the open file return?

My guess is the program is running in the compiler directory, not the source directory.

Open and write "text.txt" and find out where the file was created.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
Open and write "text.txt" and find out where the file was created.


It's running in the program directory. Mysteriously, every character returned is a -1.

epicbeast9022
Newbie Poster
15 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Okay, I've solved it ...
For some odd reason the current working directory was /Users/MYPROFILE.
[NO, I'M NOT GIVING YOU MY HOME FOLDER NAME.]
This probably only works for Macs .
I made the output and it was in ~, so I tried with the input and it worked.

epicbeast9022
Newbie Poster
15 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: