简体   繁体   中英

Is there a way to ignore end of file characters while reading files in c++?

So I am trying to read a file into a program in C++, but there are random end of files thrown in throughout the file. When trying to read the file, ifstream stops reading when it hits an end of file character. This is the code that I am using to try to read the file

size_t bytesAvailable = 1000;
std::ifstream file(directory, std::ifstream::in);
unsigned char headDataBuffer[1000];
file.read((char*)(&headDataBuffer[0]), bytesAvailable);

the file I am trying to read gets this far into the file but then stops when it reaches a certain character which I later found out to be an end of file, there is plenty of text afterwards but I can't seem to get ifstream to read anything after the end of file character. Is there a way to read the entire file without having to break it up into smaller chunks?

Firsts few lines of the file

˜1È£… .ƒÑäÄÕ;õÏ]ÀåM”Ú2jó8ÒQ;Fb#Ãë»Cé‚ 1³¸)æ¸)¼™Â¢¼mí¾J”ÜT'S·Õ}xÇ\'Ò¬Ëëk|&c õe´„[zÊN4äHH•Æpé€i‹,ɶ‰v%••¡ÁÎ:ïÂOÚåÀ‡É=wí7iÓOQ3Fg,‚¹ªGô“( stops right here ) I9á¸"æ£/¼™Ù£«|¿¿FI€À^‚ ‚2 tÁ[;Åéúî2`9es¹Va°ÝNe-˜1È´'},••°ÛÙuòŸLÚቜÕ/9ñ7,Õ[uv/†í]¼CúŸ

Try opening the file in binary mode. On some platforms, text mode and binary mode behave differently, such as the text mode interpreting end-of-line into LF, or interpreting a control character (possibly Ctrl+D or Ctrl+Z) as an end-of-file.

size_t bytesAvailable = 1000;
std::ifstream file(directory, std::ifstream::in|std::ifstream::binary);
unsigned char headDataBuffer[1000];
file.read((char*)(&headDataBuffer[0]), bytesAvailable);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM