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.
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.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 } }
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,
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?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 } }
I could always just read in one file and store it, but that doesn't seem very efficient in this case.
LMHmedchem




Reply With Quote