PapaSmurf
PapaSmurf

Reputation: 163

Is it possible to skip a line in a data file?

I have a data file that I am trying to input and the data is split into sections via a blank line. The data will be read in from a text file.

How do I make my code skip a blank line to read in the next piece of data? I am currently just in the planning stages of my application.

I'm a beginner so I'm not really sure how to go about this.

Can anyone advise a method on how to approach this?

I have just written it out and my code looks like this:

        string  ship2_id;
        char ship2_journey_id[20];
        float ship2_l; 
        int ship2_s;

        getline(itinerary_file, ship2_id);

        if (ship2_id = ' ')
        {
            itinerary_file.ignore(numeric_limits<streamsize>::max(), '\n');

        }
        else
getline(itinerary_file, ship2_id);
cout << ship2_id << endl;

Upvotes: 0

Views: 109

Answers (2)

Desaroll
Desaroll

Reputation: 333

I don't what are you using to read the file, but, to search for a blank line, look for two "line breaks" together. Take in account that the "line breaker" character is different for some OS. In Windows, by default, there are two characters that are used together for a line break.

Upvotes: 0

sehe
sehe

Reputation: 393039

Yes,

stream.ignore(max_number_of_chars_to_be_skipped, '\n');

I usually just use 1ul<<30 or similar for the first parameter, but

  • this could be a DoS vector if the input is untrusted and slow to skip those chars
  • the "pedant" value would read std::numeric_limits<std::stream_pos>::max() or similar

Upvotes: 1

Related Questions