Question

I have a file with some data in it, and I am trying to split it up into several files. Seeing the data file will help you understand.

#AFlags: -O0

    #Bytes  blowfish       AES      3DES
       1MB        60        20       280
       5MB       320        90      1400
      10MB       570       180      2720
      20MB       560       180      2740
#BFlags: -O1

    #Bytes  blowfish       AES      3DES
       1MB        60        20       280
       5MB       300       100      1440
      10MB       600       170      2680
      20MB       550       170      2790

This is what it looks like. I am trying to read this file in, then split it at each line at the #[letter]Flags. So far this is what I am doing.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <string.h>
using namespace std;

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 = "results_files/" + line_contents.substr(1,5) + "_result_data.dat";
                cout << "file_path: " << file_path << endl;

                time_data_file.open( file_path.c_str() );
                time_data_file << file << endl;

                time_data_file.close();
            }
            cout << "line_contents: " << line_contents << endl;
                file = file + line_contents;
                file.push_back('\n');


        }
        initial_cpp_time_data_file.close();
    } else {
        cout << "Unable to open the data file" << endl;
    }

    cout << "Done with format data!" << endl;
    return 0;
}

I feel like I almost have it, but there is something wrong with my logic. It is creating two files, AFlag_result_data and BFlag_result_data. AFlag_result_data is empty, and BFlag_result_data has the data I want AFlag_result_data to have in it. How to I separate the data into the correct file. Also, I could have varying amounts of lines, so I cannot hard code it to split each 6 lines.

Was it helpful?

Solution

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top