I'm trying to port some existing C++ code to OS X.
The problem is that it doesn't want to load files to read from them. The code bellow illustrates this problem; when run from Finder it outputs "Failed to open input file.", however when run from Xcode it runs correctly. Thanks in advance.

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

using namespace std;

int main()
{
	cout << "Hello World!" << endl;
	ofstream Output("Output.txt");
	if(!Output.is_open())
	{
		cout << "Failed to open output file." << endl;
		return 1;
	}
	Output << "Output";
	Output.close();
	ifstream Input("Input.txt");
	if(!Input.is_open())
	{
		cout << "Failed to open input file." << endl;
		return 1;
	}
	
	string Line;
	while(!Input.eof())
	{
		getline(Input, Line);
		cout << Line << endl;
	}
    return 0;
}

Recommended Answers

All 10 Replies

Does Input.txt exist? (remember that it's case sensitive)

Replace this (line 26-30):

while(!Input.eof())
{
    getline(Input, Line);
    cout << Line << endl;
}

with this:

while(getline(Input, Line))
    cout << Line << endl;

or this:

getline(Input, Line);
while(!Input.eof())
{
    getline(Input, Line);
    cout << Line << endl;
}

You forgot to add the line Input.open(); , same applies to Output :)

OK....
1. The file definitely exists and is not empty.
2. It works when running with Xcode but not when executed through Finder. I don't think this is a problem with the code, but something else.
3. I'm opening the files using the constructor so adding a call to open() will fail.

Try ifstream Input("Input.txt", ifstream::in); instead of ifstream Input("Input.txt"); :)

You forgot to add the line Input.open(); , same applies to Output :)

The constructor does this for you if you provide a file name.

Try ifstream Input("Input.txt", ifstream::in); instead of ifstream Input("Input.txt"); :)

Both of those are equivalent. I would be both shocked and dismayed if there were a difference.

You should check whether the program is in the same directory as the Input.txt file, if you're using an IDE, your executable is probably stored in another directory :)

I can only say that I tried it on Windows and it did work perfectly :)

You should check whether the program is in the same directory as the Input.txt file, if you're using an IDE, your executable is probably stored in another directory :)

No its definitely in the same directory as the executable, I said I've been using XCode twice now.

I can only say that I tried it on Windows and it did work perfectly :)

Which is why I said that it's not a problem with the code.

Anyway, I think I've found the solution to my problem here:

http://www.mail-archive.com/xerces-c-dev@xml.apache.org/msg15355.html

>Which is why I said that it's not a problem with the code.
Yes, but I tried it in three different ways:

  1. One time when the program wasn't in the same directory as the Input.txt file: error
  2. One time when Input.txt didn't exist in the directory where the executable was in: error
  3. And at last, when they both were in the same directory and when Input.txt exists: no error :)

Just... please no.

>Just... please no.
That was terribly informative. :icon_rolleyes:

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.