-4

I am trying to read only 10 or 100 line from my file. Is there any way that I can read certain line like this?

Ugur Yilmaz
  • 469
  • 6
  • 17
  • 1
    Please, Google and you will find EVERYTHING you need. – OhadM Oct 08 '16 at 17:15
  • Well, I already tried but could not find what I was looking for. That is why I am asking here. I did not find a solution which reads only seperate lines from a file. – Ugur Yilmaz Oct 08 '16 at 17:16
  • Really? You couldn't find [read from file only one specific line to another in C++](http://stackoverflow.com/q/23209935/62576), which I found by looking at the list of related questions to the right of this very page (below the job ads)? It was offered as a possible duplicate when you were typing your question here. You clearly didn't spend any time looking before posting here. – Ken White Oct 08 '16 at 17:32
  • Yes, but my problem is a little different. I have many lines and none of them are empty, so I cannot use any delimeters to skip lines. Obviously, I stated my problem in a wrong way. I need to create something like a loop while I am reading from a file. I was trying to ask that if there is any way to read the first 100 line or something like this. Or how can I start reading from specific line? Those are the things I spent looking for and could not find. Thanks for your help anyway. I understand that you though I did not look for a second. @KenWhite – Ugur Yilmaz Oct 08 '16 at 18:04
  • @KenWhite: In order to have a "line of text", it must be delimited, either by length or by a sentinel character (such as newline or period). Please clarify your definition of "line of text", since you can't use delimiters to skip text. – Thomas Matthews Oct 08 '16 at 18:09
  • @ThomasMatthews: I don't need to clarify anything; it's not my question. The poster asked about how to read a certain number of *lines*, so I'm presuming that it's the standard definition of a *line of text*, which is delimited by either a LF character (non-Windows) or CR/LF pair (Windows). What other definition of *line of text* would you think? – Ken White Oct 08 '16 at 18:27
  • 1
    That other post tells you how to read lines of text, and it contains a loop that reads from line 6 to line 10. Whether you have empty lines or not is irrelevant; you're still reading a line from the file, whether it's a blank line or not. If you want to read the first 10 lines, you start at line one and repeat the loop 10 times. If you want to read 100 lines, you start at line 1 and repeat the loop 100 times. If you want to read from line 10 to line 20, you read the first 9 and just ignore them, and then start paying attention when you reach line 10. – Ken White Oct 08 '16 at 18:30
  • Thanks I am going to ignore the lines before I start reading. – Ugur Yilmaz Oct 08 '16 at 18:35
  • Possible duplicate of [C++ and C file I/O](http://stackoverflow.com/questions/605839/c-and-c-file-i-o) – Ugur Yilmaz Oct 20 '16 at 07:50

1 Answers1

1

To read a single line from a file, use:

std::string text_from_file;
std::getline(text_file_stream, text_from_file);

In C++, to perform an action many times, we use a loop. So to read 10 lines from a file, we would use a for loop:

for (unsigned int i = 0U; i < 10U; ++i)
{
  std::getline(text_file_stream, text_from_file);
}

Another method:

unsigned int lines_read = 0U;
while ((lines_read < 10) && (std::getline(text_file_stream, text_from_file)))
{
  ++lines_read;
}

To read 100 lines, you would change the constant from 10 to 100.

Skipping Lines
The fundamental issue with skipping lines or seeking to a given line, is that the text file has variable length records. You will have to read each line to figure out where the next one starts.

So the technique for skipping lines is to read a line into a text variable and ignore it, much like the examples above.

There are methods to speed this up, but they involve reading large blocks of data into memory or treating the file as memory (a.k.a. memory mapping). One issue with this technique is handling the case where the text line you want crosses the end of the buffer (it is not fully in the buffer). These techniques can be found in other posts on StackOverflow or on the Internet.

Reading until a delimiter
A delimiter is something that indicates the end of text. The standard delimiter for text files is a newline. You can read text until a comma, period tab or other delimiter, by using the 3rd parameter of std::getline.

const char delimiter = '.';
std::string text_from_file;
std::getline(text_data_stream, text_from_file, delimiter);

All this is available in good text books or a good online reference.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154