CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 24

Threaded View

  1. #1
    Join Date
    May 2009
    Location
    Boston
    Posts
    375

    how does getline() know what line it's getting???

    May be a silly question, but I often use a combination of getline() and fstream in a while loop to read a delimited text file.

    Code:
       ifstream read_file;
       stringstream file_data_stream;
       string new_line, new_cell;
    
       // open the data file into file stream
       read_file.open( input_file.c_str() );
    
      // read in each line of the index file
       while(getline(read_file, new_line)) {
    
          // add current row to stringstream
          file_data_stream << new_line;
    
          // parse stringstream on tab to get fields
          while(getline(file_data_stream, new_cell,'\t')) {
             // the data is now parsed
          }
       }
    This will read through the input_file and parse it into "cells" by line and tab. There doesn't seem to be an iterator to allow getline() to keep track of where it is in the file.

    I need to read in two files so that I am reading the same line of each file one at a time. In other words, read in the first line of file 1 and then the first line of file 2. I can't see how to do this with the structure above.

    If I did something like,

    Code:
       ifstream read_file1, read_file2;
       stringstream file_data_stream1, file_data_stream2;
       string new_line1, new_cell1, new_line2, new_cell2;
    
       // open both files into file streams
       read_file1.open( input_file1.c_str() );
       read_file2.open( input_file2.c_str() );
    
      // read in each line of the first input file
       while(getline(read_file1, new_line1)) {
          // also read the second input file
          getline(read_file2, new_line2);
    
          // add current row to stringstream
          file_data_stream1 << new_line1;
          file_data_stream2 << new_line2;
    
          // parse stringstream for first file on tab to get fields
          while(getline(file_data_stream1, new_cell1,'\t')) {
             // also parse stringstream for second file
              getline(file_data_stream2, new_cell2,'\t');
    
             // the data for the same line of both files is now parsed
    
          }
       }
    Would that read both files in registration? Does getline() have an iterator somewhere I could access to instruct it to read a specific line? Am I going about this in the wrong way altogher?

    I could always just read in one file and store it, but that doesn't seem very efficient in this case.

    LMHmedchem
    Last edited by LMHmedchem; May 13th, 2012 at 09:22 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured