简体   繁体   中英

C++ Simple File I/O Issue

I have this bit of code that reads in a CSV .txt file laid out like this (the actual file is millions of rows long):

1,2,3,4,5,
6,7,8,9,10,
11,12,13,14,15,
16,17,18,19,20,
etc...

The purpose of the code is supposed to be to separate each column of the imported text file into a separate file. For example, with the above example data, text file 1 would be

1
6
11
16

Text file 2 would be

2
7
12
17

and so on...

Right now I am struggling with two things.

  1. The code for some reason skips the first value and reads the last value twice.
  2. I cannot figure out a way to read all 5 columns at once and put them into separate text files automatically. When I tried to do it before it would skip every other value for every column.

I have each "out_file" statement in the while loop commented out so that I can manually do it one by one, which would fix the skipping every other value issue, but still the 1st and last value issue exists when doing columns one by one.

Here is my code:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(int argc, char *argv[]) {
string line1;
string value1;
string value2;
string value3;
string value4;
string value5;

ifstream in_file ("in_file_comma_appended.txt");
    
ofstream out_file;

out_file.open("out_file.txt");

if (in_file.is_open())
{
    while (getline(in_file,line1))
    {
        getline(in_file, value1, ',');
        out_file << value1 << "\n";

        getline(in_file, value2, ',');
        //out_file << value2 << "\n";

        getline(in_file, value3, ',');
        //out_file << value3 << "\n";
        
        getline(in_file, value4, ',');
        //out_file << value4 << "\n";
        
        getline(in_file, value5, ','); 
        //out_file << value5 << "\n";
    }
    in_file.close();
    out_file.close();
}
    return 0;
}

Please let me know how I can resolve these issues.

Your loop is reading an entire line from the file, but not doing anything with that line, it is just thrown away. Inside the loop, you are then reading the next 5 individual values from the input file and writing them to the output file. But on the next loop iteration, you are again reading the next entire line and throwing it away. And so on. That is why you are skipping values.

Inside your loop, use a std::istringstream to read out the values from the line string that your loop condition already read, eg:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

int main(int argc, char *argv[])
{
    ifstream in_file ("in_file_comma_appended.txt");
    if (in_file.is_open())
    {
        ofstream out_files[5];
        for (int i = 0; i < 5; ++i) {
            out_files[i].open ("out_file_" + to_string(i+1) + ".txt");
        }

        string line, value;

        while (getline(in_file, line))
        {
            istringstream iss(line);

            for(int i = 0; i < 5; ++i)
            {
                getline(iss, value, ',');
                out_files[i] << value << "\n";
            }
        }
    }

    return 0;
}

I cannot figure out a way to read all 5 columns at once
... and put them into separate text files automatically.

Consider using stringstreams for intermediate values. Run them all at once if you wish... these are ram based.

stringstream ss1;
stringstream ss2;
stringstream ss3;
stringstream ss4;
stringstream ss5;


while (getline(in_file,line1))
{
    getline(in_file, value1, ',');
    ss1 << value1 << "\n";

    getline(in_file, value2, ',');
    ss2 << value2 << "\n";

    getline(in_file, value3, ',');
    ss3 << value3 << "\n";
    
    getline(in_file, value4, ',');
    ss4 << value4 << "\n";
    
    getline(in_file, value5, ','); 
    ss5 << value5 << "\n";
}

// open column 1 file
outfile1 << ss1.str(); // write col 1 data
// close column 1 file

// open column 2 file
outfile2 << ss2.str(); // write col 2 data
// close column 2 file

... Repeat this open-write-and-close 
...    for each of the other column files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM