Why ifstream::get fails to read from file?

I try to open file and print the text in "test.txt" (the file includes UTF8 characters mostlikely).

I cannot find out why nothing is printed. Also I would like to know how can I check what is the file path which the class uses. Is there any C++ constant to access this information? Or how can I find what is the problem?
Last edited on
Unicode is tricky. Maybe you should look into wchar_t and std::wifstream.


As for your method for reading the file, perhaps a more elegant approach would be using stream iterators.

recently I used several codes to read using char type and fstream or iostream and they worked. So I think that problem is not with missing ability to read the file data. Maybe the path is incorrect. In this case it prints 0 bytes read.


  if ( ifs.bad() )
   { std::cout << "Problem to read file."; }
  else if ( ifs.good() )
	  { std::cout << "First byte read."; }
  else if ( ifs.fail() )
	  { std::cout << "Fail."; }

Prints fail!

This is what I see when I watch the ifs object:
Last edited on
Even this code fails:
not using char nor wchar_t type.
can you post a sample of the text file ?
Use the full path in your code for test.txt and see if that helps.

See perror from <cstdio> in this site's reference also which can show the error for a file not opening.

Your first code will work just fine. The file you read from must be in the same directory you run your program at.
Code works. File placed there in the Debug folder.

They told me that istream does not take care about what kind of content is in the file, so it is fine to use char type.

Can you find why this one does not work to me?


Does not print anything.
I suggest you show the input file also.
Is the first line of the input file blank?
That will work properly as well. This in.txt must be in the same directory just like earlier though.
Catfish666: Ah, I see it now. There is space followed by paragraph but this prints first line only, not whole file. So that why I had seen nothing.
Last edited on
Do you have idea why this program is so slow? Testing source file is less then 1kbyte. But it is slowly printed. Is it simply because it prints every single character just after a single character is read? I mean, probably if I would read whole file to a variable and then print it immediately from the variable, that should be faster, huh?
Last edited on
You are right: it does not make any sense to read one character at a time if you are going to read a block of data anyway. Not only is that slower it is also more writing.

EDIT: Yea, and printing one character at a time does not make any sense either.
Last edited on
OK, thanks for help and answering my questions.
You are more than welcome.
Topic archived. No new replies allowed.