Neophyte to C++. Trying to reference a specific filename from within the program as opposed to asking user to input file name. Keep compiler messages referring to invalid conversion from 'char' to 'const char*. Been fighting this out for several hours referencing books and still have no success. Apparently I need someone to spell it out for me. It would be very much appreciated.
The file I want to open is called "history.txt"
code for this section that I've been trying to make work is something like this:
Spoke to soon. It does build without any error messages. Unfortunately something isn't working. The file that is supposed to be referenced isn't being referenced. By that I tried for some output from the file. It kept giving me "0" for readings. So I completely removed the file "history.txt" from the computer.
I have an "if" statement checking for the file.
If (! jimfilename)
{
then cout << "File " << jimfilename;
cout << " could not be opened" << endl;
return -1;
}
That should trigger a halt since there is no file on the computer labeled "history.txt"
but it goes right to the "else" part of the statement of giving me values I asked for based on reading lines. Unfortunately those values are not correct.
First off, that little snippet shouldn't compile if that's what you have in your code. then? ;)
Second, did you mean !filein.is_open() for the condition of your if statement, by any chance? What you're basically doing there is checking if jimfilename is not equal to zero. >_>
Apparently the file isn't opening. How does one open a file in this case?
The code currently looks like this:
1 2 3 4 5 6 7 8 9 10
ifstream jimfilename("history.txt");
if (! jimfilename.is_open())
{
cout << "File " << jimfilename;
cout << " could not be opened" << endl;
return -1;
}
else
{// the else side of the "if" statement
I am getting a terminal readout: File 0 could not be opened
But the thing about it is there is "History.txt" present now in the directory.
as you can see the code keeps evolving as I keep reading and reading and reading. I appreciate the help.
Thank you. I know old dogs can learn new tricks it just takes awhile longer.
Have you checked that you are trying to open "history.txt" and not "History.txt"
as the name you try to open must exist. "history.txt" as your program stands it
should work. Provided the file "history.txt" exists in your current directory.