Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Convert 5 column data in to 2 column data

I have a file and I want to read it and create an output file according to the description given below.

The first row of the input file has 2 numbers. The value of the first number(in the 1st row and 1st column) is irrelevant for our purposes. The value of the second number(in the 1st row and 2nd column) is however important.

So for example in the attached input file the second number is 399. This means the output file will have 399 + 1 rows.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The first row of the output file will contain exactly the same line as the first row of the input file.

Next from the second row onwards the input file has 5 columns. What I want is that we should read the values from these columns(1st to 5th column) 399 times and store them as the 1st column of the output file.

Now after that, the remaining values(also 399 in number) will be stored in the output file as its 2nd column.

For reference, I have given example input and output files.

input file

24  399
0.4000000E-01 0.6001000E-01 0.7999000E-01 0.8997000E-01 0.9999000E-01
0.1400000E+00 0.1800000E+00 0.2200000E+00 0.2600000E+00 0.3000000E+00
0.3400000E+00 0.3800000E+00 0.4200000E+00 0.4601000E+00 0.4999000E+00
0.5400000E+00 0.5799000E+00 0.6199000E+00 0.6598000E+00 0.7001000E+00
0.7402000E+00 0.7798000E+00 0.8200000E+00 0.8598000E+00 0.8997000E+00
0.9198000E+00 0.9596000E+00 0.9999000E+00 0.1040000E+01 0.1080000E+01
0.1120000E+01 0.1160000E+01 0.1200000E+01 0.1240000E+01 0.1280000E+01
0.1320000E+01 0.1360000E+01 0.1400000E+01 0.1440000E+01 0.1460000E+01
0.1500000E+01 0.1520000E+01 0.1770000E+01 0.2030000E+01 0.2130000E+01
0.2220000E+01 0.2330000E+01 0.2420000E+01 0.2530000E+01 0.2630000E+01
0.2720000E+01 0.2830000E+01 0.2920000E+01 0.3030000E+01 0.3130000E+01
0.3220000E+01 0.3330000E+01 0.3420000E+01 0.3530000E+01 0.3630000E+01
0.3720000E+01 0.3830000E+01 0.3920000E+01 0.4029000E+01 0.4130000E+01
0.4220000E+01 0.4320000E+01 0.4400000E+01 0.4500000E+01 0.4601000E+01
0.4700000E+01 0.4800000E+01 0.4901000E+01 0.4999000E+01 0.5100000E+01
0.5209000E+01 0.5301000E+01 0.5391000E+01 0.5510000E+01 0.5610000E+01
...
...
...
0.1522580E-06 0.1430180E-06 0.1343340E-06 0.1261710E-06 0.1185000E-06
0.1112900E-06 0.1045140E-06 0.9814640E-07 0.9216380E-07 0.8654260E-07
0.8126080E-07 0.7629820E-07 0.7163560E-07 0.6725600E-07 0.6314150E-07
0.5927650E-07 0.5564600E-07 0.5223600E-07 0.4903330E-07 0.4602550E-07
0.4320050E-07 0.4054730E-07 0.3805600E-07 0.3571640E-07 0.3351950E-07
0.3145680E-07 0.2951990E-07 0.2770140E-07

The 3 dots in the above input file are just to tell you that there are many more entries in the file. These dots are not there in the original file.

The total number of lines in the input file is 161 including the first row that has 24 399.

expected output file

24 399
0.4000000E-01 0.9710830E-04
0.6001000E-01 0.3634790E-03
0.7999000E-01 0.2970320E-03
0.8997000E-01 0.4819220E-03
0.9999000E-01 0.6664240E-03
0.1400000E+00 0.9319220E-03
0.1800000E+00 0.8522330E-03
0.2200000E+00 0.1070000E-02
0.2600000E+00 0.1410000E-02
0.3000000E+00 0.1960000E-02
0.3400000E+00 0.2990000E-02
0.3800000E+00 0.4890000E-02
0.4200000E+00 0.7280000E-02
0.4601000E+00 0.9280000E-02
0.4999000E+00 0.1116000E-01
0.5400000E+00 0.1302000E-01
0.5799000E+00 0.1467000E-01
0.6199000E+00 0.1620000E-01
0.6598000E+00 0.1806000E-01
0.7001000E+00 0.2001000E-01
0.7402000E+00 0.2166000E-01
0.7798000E+00 0.2316000E-01
0.8200000E+00 0.2413000E-01
0.8598000E+00 0.2455000E-01
0.8997000E+00 0.2450000E-01
0.9198000E+00 0.2463000E-01
0.9596000E+00 0.2486000E-01
0.9999000E+00 0.2525000E-01
0.1040000E+01 0.2540000E-01
0.1080000E+01 0.2539000E-01
0.1120000E+01 0.2555000E-01
0.1160000E+01 0.2560000E-01
0.1200000E+01 0.2572000E-01
0.1240000E+01 0.2590000E-01
0.1280000E+01 0.2596000E-01
0.1320000E+01 0.2603000E-01
...
...
...
0.2514620E+05 0.5564600E-07
0.2555290E+05 0.5223600E-07
0.2596620E+05 0.4903330E-07
0.2638610E+05 0.4602550E-07
0.2681290E+05 0.4320050E-07
0.2724660E+05 0.4054730E-07
0.2768730E+05 0.3805600E-07
0.2813510E+05 0.3571640E-07
0.2859020E+05 0.3351950E-07
0.2905260E+05 0.3145680E-07
0.2952250E+05 0.2951990E-07
0.3000000E+05 0.2770140E-07

The total number of lines in the output file is 399 + 1 = 400 including the first row that has 24 399.

Summary
So basically we have to arrange the 5 columns into 2 columns. If you still have any queries about how the output file should be generated then you can look at the input and output files’ starting and ending values.

>Solution :

This can be done using a std::vector of std::pairs where the pair 1st element will correspond to x-value and the pair’s 2nd value will correspond to y-valuee as shown below. You can use this program as a reference(starting point) for your future C++ based programs.

#include <iostream>
#include <vector> //needed for creating  a vector
#include <sstream>//needed for std::istringstream 
#include <fstream>//needed for reading the input file and writing the output file


int main()
{
    std::ifstream inputFile("input.DAT");//create open the input file for reading
    
    int k = 0, n = 0;//this variable will be used to reserve vector size
    std::string line; //this variable will be used to read line from input file 
    std::vector<std::pair<std::string, std::string>> myVec; //this is a vector of pairs. Each pair has the first element as x-value and second element as y-value
    std::string word;//this variable will be used to read a particular line column by column(actually word by word)
    if(inputFile)//check if the input file opened correctly
    {
        int count = 1;//this is not needed at all. Just remove this line. I accidentally added it and forgot to remove it
        
        //read the very first line 
        std::getline(inputFile, line);

        
        std::stringstream ss(line);
        
        //take the first number into variable k. For example in this input.DAT file this value is 24
        ss >> k;
        //take the second number into variable n. For example in this input.DAT file this value is 399
        ss >> n;
        
        //use reserve 
        myVec.reserve(n);
        
       
        int i = 0;//this variable is used to keep track of that fact that we don't go over 399 times
        //now start reading line by line from 2nd row onwards
        while(std::getline(inputFile, line))//now we start reading from 2nd row onwards
        {
             std::stringstream ss(line);
             while( ss >> word)//read word by word(column by column)
             {
                 if( i < n)//this if condition checks whether we have read all(399) the x-values. This is the main part of the program
                 {
                     // ss >> word;
                      myVec.emplace_back(std::make_pair(word, ""));//here first we create a std::pair that has the x-value as the word just read and the y-value as an empty string. Then we add this pair of x-value y-value into the vector
                      
                      
                 }
                 else if((i >= n) && (i < 2*n))//this condition is satisfied when we have completed reading the first 399 values and now we want to start reading the y-values.
                 {
                     //ss >> word;
                     myVec.at(i - myVec.size()).second += word;//here we just append the word just read into the empty strig y-value that was set in the previous if block
                 }
                 ++i;
             }
             
            
        }
        std::ofstream outputFile("output.DAT");//create an ofstream object that will be used to write the output file
        outputFile << k << " " << n << "\n";
        //now loop over all pairs of x-value y-value 
        for(const std::pair<std::string, std::string> &myPair: myVec)
        {
            //write the pair x-value and y-value into the output.DATA file       
            outputFile << myPair.first << " " << myPair.second << "\n";
        }
        
        outputFile.close();
    }
    else 
    {
        std::cout<<"input file cannot be opened"<<std::endl;
    }

    return 0;
}


Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading