reading any file

hi,all

I'm new to c++ and I want to read any file byte byte and then get the ASCII code fore each file.

I searched in Google and also search this forum but I couldn't find any thing useful.

that's what I did until now but unfourtonatly it doesn't works.

1
2
3
4
5
6
7
8
9
10


	ifstream myfil("F:\\2nd year_2\\Data structure 2\\books\\New Text Document");
if(myfil.is_open(){
char ch;
myfil.get(ch);
cout<<(int )ch<<endl;
}

	 


negative number will be printed and when I casted it to unsigned int,it will very big number greater than 256

Thanks in advance
You might want to check if there is an error on your fstream before trying to print the char that youattempt to read:
1
2
3
4
5
6
7
8
9
std::ifstream ifs("data.txt");

char c;
if(ifs.get(c))
{
    // only gets here if the read was successful
    // (the file was opened and was not empty or non-readable etc...)
    std::cout << int(c) << '\n';
}
Last edited on
Topic archived. No new replies allowed.