0

enter image description hereI am trying to read File which has Ascii data inside file. It deosnt read full file and stops in between(when I checked in file by converting to Hex format, it shows it get stop when enters 0x1a). Below is my code

int _tmain(int argc, _TCHAR* argv[])
{

      ifstream infile;
      char c;

       infile.open("test.txt");

       while(!(infile.eof())){

             infile.get(c);

             cout<< c;

       }

return 0;

}

Please suggest me how to read entire file(data inside file is in ascii format)?. Thanks in advance

Text File data : ¯µbë éÿïÿ ¬F¸

The reading has stopped reading after ey.

akash patil
  • 21
  • 1
  • 6
  • 1
    btw, `ifstream::get()` expects a reference to the `char` as a parameter. – Federico klez Culloca May 08 '17 at 10:19
  • How long is `test.txt`? Can you reproduce the problem with a short file (a few lines)? If so, include the content of that file in the question, and also show the output. – slim May 08 '17 at 10:20
  • 1
    0x1A is not a printable ASCII character. Are you sure the input file is ASCII text? – slim May 08 '17 at 10:23
  • 2
    [Why is `iostream::eof` inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – molbdnilo May 08 '17 at 10:27
  • yes it is ascii text. example of file is µbƒ éÿ üÿÿüÿ ÿÿîÿeÚ´ ïÿ . – akash patil May 08 '17 at 10:35
  • file size is 48kb only. – akash patil May 08 '17 at 10:35
  • There is no `é` or `ÿ` in ASCII. This is not ASCII text. It might not be the cause of the problem, but it means your question is not accurate. – slim May 08 '17 at 10:37
  • Try and reproduce the problem with a 10 byte file -- just trim it down to the part where you see it fail. Then copy/paste the whole 10 bytes into the question (not a comment) and also copy/paste the console output when you run the program. Then we have a MVCE https://stackoverflow.com/help/mcve. – slim May 08 '17 at 10:42
  • ... and if you can't reproduce the problem in a 10 byte file, explain the details of that in the question. – slim May 08 '17 at 10:42
  • 1
    `0x1A` or `Ctrl-Z` can be seen as end-of-file in Windows (which I'm guessing you're using) when a file is opened in "text mode". Try opening the file in _binary_ mode (I think by using `ios_base::binary` as the second parameter to `open`). – TripeHound May 08 '17 at 11:35
  • Yes I have changed to binary mode and it is working fine. Thanks tripeHound – akash patil May 08 '17 at 13:23

0 Answers0