File I/O help needed

This program below returns -1 everytime. However, it should retun 5 as the file "Hello.txt" has the word "Hello" which has length 5.


#include<iostream>
#include<fstream>
using namespace std;

void main()
{
ifstream File("d:\Hello.txt");
char arr[10];
File.read(arr,10);

//this should return 5, as Hello is 5 characters long
cout<<File.tellg()<<endl;
File.close();

system("pause");
}
You asked for 10 characters from the file stream which only contained 5. This flagged an error on the file stream.

http://cplusplus.com/reference/iostream/istream/read/

After the error is flagged tellg() always returns -1:

http://cplusplus.com/reference/iostream/istream/tellg/


Topic archived. No new replies allowed.