I have some code that is supposed to transfer all the code from one text document to another, but replaces certain strings of characters with another. I've checked my code several times, and I figured out that my problem was .eof() andmy file. I know how to use fstream and .eof(), but the program won't read my file at all. I don't even know where to start. Does anyone know why it might not read my file?
load.open("pokeMoves.txt");
if(!load.is_open())
{
cout << "Could not open pokeMoves.txt\n";
return 0;
}
else
{
change.open("pokemon moves.txt");
// we are not checking to see if this opening cleanly... the file name seems suspect.
while(!load.eof())
{
getline(load, compare);
if(compare != number)
{
change << compare;
cout << compare << endl;
}
elseif(compare == number)
{
change << changing;
cout << compare << endl;
}
else
{
cout << "What?\n";
}
} //while
load.close();
change.close();
} // else on open.
this assumes you have one number per line or a string or string of information because I don't see the definition of things. My thought is something didn't open cleanly and is causing errors.
Change is an ofstream variable, load is an ifstream variable, and all the rest of the variables are strings. I don't worry about the newline character, but I do worry about why it's reading that I'm at the end of a full file right after opening it.
EDIT: I used an if statement to make sure that change opened "pokemon moves.txt" and close if it didn't. When I ran it again, I still got an .eof() = 1 even though it was opening and I know that it has data in it.
Try using while(load.good()) and see if there is any difference.
Edit: Would you be able to upload the input file somewhere? I am not able to reproduce the error.
I am starting to suspect it is compiler and development platform stuff. I am not sure how getline is coded but I have never had an issue getting a line from a file and parsing from there in a string. I am not sure what would invalidate the stream, unless you are reading a wide char or unichar file and it fills something incorrectly. Other issues like something that screw it the file pointer in the os may doing something.
I can't figure out what the problem is then because it's an ordinary text file and it has a ton of information. The only thing I can think of that might make this not work is if it's too big, but I highly doubt that because I don't think file size is a factor on whether or not the program can access it unless I make file size a factor or the file is too big for the computer itself. I don't know how to make file size a factor and the file itself is probably less than a megabyte.
I see what you did. I know why the file is at eof.
I would break up the two load files into two different file stream and not use one Load class for two different files. It appears that the Load file object isn't resetting completely on a new file open. It might require a Load.seek(std::ios::begin) or something similar. Because I am suspecting the file pointer of old file is interfering with file pointer of new file and if the sizes aren't exact the eof flag would be set in certain cases. It is easier to make independent objects for all files you open because this could be something with the OS implementation of the stdc++ lib or something that wasn't included in ios classes and may not be covered in the standard.