I'm trying to read a text file that contains only strings. It is compiling and opening but when reading it just displays junk that has no bearing to the characters in the file at all.
Can anyone see what's wrong?
#include <iostream> #include <fstream> using namespace std; fstream myfile; char* input; void main(void) { myfile.open("H://fstream_test.txt", fstream::in); if(myfile.fail()) { cout<<"error"; exit(0); } cout<<"file is open"; int beginning = myfile.tellg(); myfile.seekg(0, fstream::end); int end = myfile.tellg(); int size = end-beginning; cout<<size; //returns 15 input = new char[size]; myfile.read(input,size); cout<<input; //returns junk //for(int i=0;i<size;i++) //cout<<input[i]; //returns the same; } end edited to:
input = new char[size]; myfile.seekg(0, fstream::beg); while(!myfile.eof()) { myfile.read(input,size); } cout<<input; system("pause");