using fstream to read every character including spaces and newline

前端 未结 13 1571
一个人的身影
一个人的身影 2020-12-07 22:47

I wanted to use fstream to read a txt file.

I am using inFile >> characterToConvert, but the problem is that this omits any spaces an

13条回答
  •  一个人的身影
    2020-12-07 23:19

    A lot of the benefit of the istream layer is providing basic formatting and parsing for simple types ro and from a stream. For the purposes that you describe, none of this is really important and you are just interested in the file as a stream of bytes.

    For these purpose you may be better of just using the basic_streambuf interface provided by a filebuf. The 'skip whitespace' behaviour is part of the istream interface functionality that you just don't need.

    filebuf underlies an ifstream, but it is perfectly valid to use it directly.

    std::filebuf myfile;
    myfile.open( "myfile.dat", std::ios_base::in | std::ios_base::binary );
    
    // gets next char, then moves 'get' pointer to next char in the file
    int ch = myfile.sbumpc();
    
    // get (up to) the next n chars from the stream
    std::streamsize getcount = myfile.sgetn( char_array, n );
    

    Also have a look at the functions snextc (moves the 'get' pointer forward and then returns the current char), sgetc (gets the current char but doesn't move the 'get' pointer) and sungetc (backs up the 'get' pointer by one position if possible).

    When you don't need any of the insertion and extraction operators provided by an istream class and just need a basic byte interface, often the streambuf interface (filebuf, stringbuf) is more appropriate than an istream interface (ifstream, istringstream).

提交回复
热议问题