Question:
C++ Reading a text file?
Vise
2018-03-22 04:29:20 UTC
So lets say I have a text file of strings. Lets say I read half of the text file in an array of strings or some kind of buffer. And then I do some operations on that array.
Then lets say a want to go back to my text file and start reading the text file from where I left off. How would I do that? How do I tell the program to start off where I last stopped reading in a text file. I do not need the code necessarily but rather the process/logic.
Thanks.
Three answers:
Laurence I
2018-03-22 10:41:21 UTC
when you open a file it has a channel number. you can then perform operations on that channel. operations include open/close/rewind/read/write/truncate/create. so long as you havnt closed the channel, the next read will continue from the same place. If you mean you want to close the program and restart it from the same cursor(character) position if you like, then YOU are responsible for that. most files can be opened in record mode or binary mode. in record mode(lines of text) you simply keep a count of the lines you have read. in Binary mode you have to deal with each character/bytes and read blocks of them into a buffer, so you would have to keep track of how many buffer fulls you had read, and how many bytes(characters) into the buffer you had processed so far(last time). How this translates to C++ code can be seen at the link below.
husoski
2018-03-22 15:09:15 UTC
If you need to remember your place in a stream, you can use the tellg() method to get the input position (for stream, istream and subclasses) in a stream you are reading; or tellp() to return the output position in a stream you are writing (stream, ostream, and subclasses.) The return value is a position relative to the beginning of the file (an exact offset in bytes for a file opened in binary mode, but this isn't guaranteed to be exact for text mode streams.)



Later, you can use the seekg() or seekp() method to restore the position, using the position returned byte tellg/tellp as an offset from the beginning of the file. For example, suppose infile is an ifstream object opened on a file. Then:



auto position = infile.tellg(); // saves the current input position



The g and p suffixes seem to be abbreviations for "get" (for reading) for and "put" (for writing).



Later, perhaps after reading from or closing and reopening the stream on the same file, you can use:



infile.seekg(position, inflie.beg); // get next input starting at (position) relative to the beginning of the file



The type of the returned position is std::streampos. It behaves a bit like an integer, in that you can add or subtract std::streamoff offset values to or from it. It's usually a bad idea to do arithmetic like that on text-mode streams, but that does let you compute read locations in a binary stream.



See:

tellg: http://www.cplusplus.com/reference/istream/istream/tellg/

seekg: http://www.cplusplus.com/reference/istream/istream/seekg/



...with examples of using seekg/tellg to find the length of a binary file and using that to read the whole file into memory.
kathys
2018-03-22 08:40:42 UTC
File streams opened in binary mode perform input and output operations independently of any format considerations. Non-binary files are known as text files, and some translations may occur due to formatting of some special characters (like newline and carriage return characters). Since the first task that is performed on a ...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...