0

I have a data set with headers and data below those headers. How do I get c++ to read the first line of actual data (which starts on the 3rd row) and keep reading until the file ends?

I know you have to use a while loop and '++' on some declared variable, but I'm not sure how to.

Here is a screenshot of the data file: enter image description here

Rohit Gupta
  • 2,411
  • 11
  • 21
  • 36
Chris Lim
  • 52
  • 7
  • 1
    Please do not use links in you post. You could just type in a few lines of the data (or copy/paste it) – Rohit Gupta Oct 13 '15 at 02:40
  • Welcome to Stack Overflow. Please edit the question and supply the first few lines of your data in text form. Also, you should post what you have tried so far. – Rohit Gupta Oct 15 '15 at 03:37

1 Answers1

1

Just read the first line into a dummy variable first before your while loop

How to read line by line or a whole text file at once?

#include <fstream>
#include <string>

int main() 
{ 
    std::ifstream file("Read.txt");
    std::string str; 
    std::getline(file, str); // read a line, as dummy read
    while (std::getline(file, str)) // keep reading till end of file
    {
        // Process str
    }
}
Community
  • 1
  • 1
Porz
  • 153
  • 1
  • 11
  • that's the part I don't understand. So you mean something like: row = 1; while (condition) row++ etc...? How would the program understand that "row" is being used to indicate a row that it's reading? – Chris Lim Oct 13 '15 at 02:41
  • getline just get the next line, so you don't need to worry about row++ – Porz Oct 13 '15 at 02:46
  • will this work if I don't want to read each piece of data in the row? For example, if a row has data for "Name", "address", and "job", but I only wanted the "name" and "job" data entries, will getline work? – Chris Lim Oct 13 '15 at 02:50
  • Think you need to read the whole line first then strip out other "useless" data. Or do you have existing function to read your row and return you only the needed data entries? – Porz Oct 13 '15 at 02:53
  • Search for file / string parsing. There are different methods that work. The easiest way is to read in one line of text saving it to a string. Then use the std library to parse that line of text breaking down different parts of that line into tokens (valuable or useable data) that you will in return store into your data structure, process or manipulate the data. Then repeat this process until you have reached either the end of the file or a "keyword - grammar" that signals the end of the file that you want to read which will cause you to break out of your loop ignoring the rest of the file. – Francis Cugler Oct 13 '15 at 03:55