Am trying to read data from a file and store it into a char array. Mostly successful but I get a weird output. The code function looks like this:
char* read_answers() { string fileName, data; char* answer = new char [50]; ifstream inFile; while(!inFile.is_open()){ //cout<<endl<<"Please enter the name of the answers file: "; //cin>>fileName; inFile.open("answers.txt"); if (!inFile.is_open()){ cout << "Error opening file" << endl; } } for (int i = 0; i < 50; i++) { if (inFile.eof()) continue; inFile.get(answer[i]); cout << i << answer[i] << endl; } inFile.close(); cout << answer[0]; cout << answer[1]; cout << answer[2]; return answer; } The file looks like this:
C A C A B D D and the output looks like this:
0C 1 2A 3 4C 5 6A 7 8B 9 10D 11 12D 13 14═ C A To me it looks like it is storing empty data into the array. Is there anyway to skip reading the blank data?
end of linesin the filewhile (!stream.eof())) considered wrong?