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;
}

Recommended Answers

All 5 Replies

Member Avatar for iamthwee

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");

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.

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.