This seems simple, but it's not working. There IS a text file in the same directory, but it's not being opened (and with the ios::in | ios::out, it should open even if it doesn't exist).

I also ran into trouble trying to use infile.fail, which is why I used !infile.

Thanks for any help!

Rich

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

int main () 
{
	// inputting a char from a file to a var and the console
	char ch; // The test file has one character, 'A'.
	fstream infile;
	infile.open("charsfile.txt", ios::in | ios::out);
	if (!infile)
		cout	<< "File didn't open"	<< endl;
	infile.get(ch); 
	cout	<< "The test character is: "	<< ch	<< endl;
	
	return 0;
}

Works for me.

Doublecheck the spelling of your data file name, does it agree with what's in the code?

Is the file in fact where your program is expecting it to be? What environment are you using, how are you executing the program?

I suggest:

infile.open("C:\\pathnameToFile\\charsfile.txt", ios::in | ios::out);

to make sure you can see the file during development.

Works for me.

Interesting!

It's not the spelling (I tried copying/pasting, same results).
I'm not sure how to use my environment (Apple's XCode IDE) to ascertain where the program is looking to find the file. I've tried putting the file next to the project folder, then inside it, so far with the same results. Of course there are folders within folders... Any ideas?

Rich

I tried this: C:\\richmansfield\\charsfile.txt

then I moved the file inside the project folder and tried this:
C:\\richmansfield\\File Input Output\\charsfile.txt

Neither one worked. Any other ideas?

I tried this: C:\\richmansfield\\charsfile.txt
[...]

Any other ideas?

Yeah I do, but you won't listen.

Am I missing something or did he just say he tried your suggestion?

@OP:
Are you sure that you have the proper rights to the directory and/or file you're trying to open?

[edit]
What's your OS?

Edited 6 Years Ago by Nick Evan: n/a

What's your OS?

OP identified it as Apple (XCode).

Richman - if you want to know where your IDE is looking for files, try making a little program that creates one, without specifying any path. Where does that file appear?

Doesn't the Mac OS use forward slashes instead of backslashes in file paths? / versus \

I assume that your code outputs "cannot open file" and does not crash

it is normally a good idea to put markers next to chars when outputting as there are many chars that appear as space and sometimes buffers hold onto return characters for example.

I normally use is_open() method to check

One thing to try is to write a file using

#include <fstream>
#include <string>
#include <iostream>
int main()
{
std::string file_name("test_file.txt");
std::ofstream fout(file_name.c_str(), std::ios::out);

if(fout.is_open() == true)
{
//currently doing nothing other than create the file
fout.close();
}
else
{
std::cout << "could not open file!" << std::endl;
}

return 0;
}

the idea is to see where the file writes if it works but you can't see
the file search for it. In XP some applications can lock the file and stop you from opening another problem can be that characters in your string might not be what you think they are as wchar can look identical to chars and your compiler just assumes that the name is correct.

Hi - I copied and pasted your code in, and it compiled, but when I chose build and run, no file seems to have been written, but neither did I get the "could not open file" message (in my version, I did get the "File didn't open" message). I did a search for the file on the hard disk anyway, but got zero hits. I'm thinking I'll try it on the computers at the school lab next; might have better luck there, and that'll tell me something.

This is unusual behaviour that you are getting and I would make sure as others mentioned that you are writing to a place that you should be able to write. I have had unusal interactions occasionally in the past and to minimise you could consider the following steps

It might be worth:

- creating a new project for the main in case their is some IDE corruption

- use an explicit file location equivalent to your My documents
C:\\Documents and Settings\\user\\

the \\ is needed for a single \
output the filename to screen to check the location

check that you can create a file in this folder using some basic program or copy and paste.

put everything in a try catch block and see if an exception occurs

I am not familiar with your OS and it could just be that the IDE does something odd to prevent the executable running but it doesn't sound like it should be behaving the way it is.

Hi - I copied and pasted your code in, and it compiled, but when I chose build and run, no file seems to have been written, but neither did I get the "could not open file" message (in my version, I did get the "File didn't open" message). I did a search for the file on the hard disk anyway, but got zero hits. I'm thinking I'll try it on the computers at the school lab next; might have better luck there, and that'll tell me something.

Hi, Tetron et al - It certainly IS unusual behavior. I tried it on a different IDE at school, on a Windows computer, and it worked fine. I must've done something to annoy either my IDE or the OS, but whatever it was doesn't repeat with other programs. I've had the same experience in reverse as well.

I'm marking it as solved, since the code works. Maybe someday I'll know enough to get beyond that and really understand the machine!

Thanks,

Rich

This question has already been answered. Start a new discussion instead.