I'm trying to read a series of char letters (one per line) from a file and into an array. For some reason, instead of reading the letters, it's just reading/showing "Ì".
The file "CorrectAnswers" contains 20 letters, one character per line. The compiler does not display any errors, but, as stated before, everything comes out as "Ì" on the console as well as the output file.
Any helps/pointers will be appreciated!
Oh and yes, this only happens for char. If I used a different file with only numbers, and declare the array as an int, it works perfectly and displays the numbers.
remember to close the file after reading in the info. Also try putting in the full path to the file just to make sure your compiler is looking in the right place.
// print the content of a text file.
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main () {
std::ifstream ifs;
ifs.open ("test.txt", std::ifstream::in);
char c = ifs.get();
while (ifs.good()) {
std::cout << c;
c = ifs.get();
}
ifs.close();
return 0;
}
and by the way, your above code from the first example works fine for me.
Although it tries to print out more of the array even if only part of it is full...the last characters come out something strange. But your code compiles and runs with a text file of characters in the same folder.
So, I had it checked out by one of our TA's. It turns out that something was wrong with the file themselves.
What was wrong with the file? We don't know. But I'm assuming there was an error in reading the file. The above code worked when we started everything from scratch (program as well as creating new original files).