0

I am able to read my text file. Now i would like to parse the string line by line. I am using header file and cpp file.. can anyone help me with parsing tutorial. Where can find a good tutorial for parsing?

DVK
  • 126,886
  • 32
  • 213
  • 327
user512698
  • 51
  • 3
  • 5
  • What do you mean "parse the string line by line"? Can you show us a sample text file you wish to parse? If not obvious, tell us what it means. – wilhelmtell Feb 18 '11 at 18:23
  • I have a 100 line which has say for example: student name,roll number, dob, address etc.i have to parse these infinite lines and keep them in database. – user512698 Feb 18 '11 at 18:57
  • Which makes this a duplicate of http://stackoverflow.com/questions/415515/how-can-i-read-and-manipulate-csv-file-data-in-c. – Tergiver Feb 18 '11 at 19:08

4 Answers4

1

You can try http://www.cppreference.com/wiki/ and look at examples of using stringstreams.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
0

I don't see what this has to do with header files, but here's how you parse a stream line by line:

void read_line(std::istream& is)
{
  // read the lisn from is, for example: reading whitespace-delimited words:
  std::string word;
  while(is >> word)
    process_word(word);
  if( !is.eof() ) // some other error?
    throw "Dude, you need better error handling!";
}

void read_file(std::istream& is)
{
  for(;;)
  {
    std::string line;
    if( !std::getline(is,line) )
      break;
    std::istringstream iss(line);
    read_line(iss);
  }
  if( !is.eof() ) // some other error?
    throw "Dude, you need better error handling!";
}
sbi
  • 219,715
  • 46
  • 258
  • 445
0

Try this:

#include <iostream> 
#include <vector>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream fs("myFile.txt");
    string input;
    vector<string> sets;
    while( getline(fs, input) )
        sets.push_back(input);
}
0

First you need to know if the lines contain fixed length fields or are the fields variable length. Fixed length fields are usually padded with some character such as spaces or zeros. Variable length fields are usually terminated by a character such as a comma or tab.

Variable Length Fields

Use the std::string::find or std::string::find_first to find the ending character; also account for the end of the string as the last field may not contain the terminating character. Use this position to determine the length of the field (ending field position - starting field position). Finally, use std::string::substr to extract the field's content.

Fixed Length Fields

Use the std::string::substr method to extract the text. The starting and ending positions can be calculated using the accumulated lengths of the previous fields, if any, and the size of the current field.

Converting Field Text

The contents of the field may not be a string and will need to be converted to an internal data type. For example, a number. Use std::istringstream to convert the text of the field to an internal data type.

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