When you read the first line containing Flags
, file_content
is empty. That's why there's nothing in the first file and the content that should be in the first file shows up in the second file.
The following tweaked version of main
works for me:
int main( int argc, char* argv[] ) {
string line_contents, initial_cpp_time_data, compare, find, file_path, file = "";
ofstream time_data_file;
//Grab the data from the old file
ifstream initial_cpp_time_data_file ( "cpp_time_data.txt" );
if (initial_cpp_time_data_file.is_open()) {
while ( getline( initial_cpp_time_data_file, line_contents ) ) {
compare = line_contents.substr(2,5);
if ( strcmp( compare.c_str(), "Flags" ) == 0 ){
file_path = line_contents.substr(1,5) + "_result_data.dat";
cout << "file_path: " << file_path << endl;
if ( time_data_file.is_open() )
{
time_data_file.close();
}
time_data_file.open( file_path.c_str() );
}
cout << "line_contents: " << line_contents << endl;
time_data_file << line_contents << std::endl;
}
if ( time_data_file.is_open() )
{
time_data_file.close();
}
initial_cpp_time_data_file.close();
} else {
cout << "Unable to open the data file" << endl;
}
cout << "Done with format data!" << endl;
return 0;
}
Update
To deal with empty lines, change the line
compare = line_contents.substr(2,5);
to
if ( line_contents.size() >= 5 )
{
compare = line_contents.substr(2,5);
}
else
{
compare.clear();
}