简体   繁体   中英

Reading multiple .txt files c++ linux

am trying to read multiple .txt file and push_back each line from each text to a vector of type string. hence: the first file have 200 lines. the second file have 800 lines.

but, i have a problem to read the second file until it end.

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <vector>

using namespace std;


struct data
{
  string from_file_1;
  string from_file_;
};

int main()
{
data my_data;
string file_1="file1.txt";
string file_2="file2.txt";

ifstream file_one(file_1.c_str);
ifstream file_two(file_2.c_str);

Vector<data> mydata;
int  max_chars_per_line=100000;
    while(!file_one.eof()&&!file_two.eof())
    {
            char buf[max_chars_per_line];
            file_one.getline(buf, max_chars_per_line);
            string str(buf);

            char buf2[max_chars_per_line];
            file_two.getline(buf2, max_chars_per_line);
            string str2(buf2);

           my_data.from_file_1=str;
           my_data.from_file_2=str2;

           mydata.push_back(my_data);
    }
//when loop exits, the size of the vector ,mydata, should be greater than 200+, but doesn't work .
return 0;
}

thank you for your time to help me.

You need to check for end-of-file from either file and the best way to detect end-of-file is by checking the result of getline() . This code also reads directly into the instance variables of data rather than using an intermediate character buffer.

Vector<data> mydata;
data data;
while (getline(file_one, data.from_file_1) &&
       getline(file_two, data.from_file_2))
{
    mydata.push_back(data);
}

Change

while(!file_one.eof()&&!file_two.eof())

to

while(!file_one.eof() || !file_two.eof())

You're going to need to check for end-of-file before reading each file, and make sure your str1 and str2 are empty if there is nothing to read

You need to fix your condition because your conditions are in "AND" so when the first file ends, the second file's lines won't be added at all.

Why don't you use a single vector in which you put all the lines that you've read? In this way you can easily split the reading phase. You will have two while loop, one for each file without having any other problems. In each while you will do this kind of operation on a single vector my_data :

while(!curr_file.fail()) {
    char buf[max_chars_per_line];
    file_one.getline(buf, max_chars_per_line);
    string str(buf);
    my_data.push_back(buf);
}

You should not be checking eof() as this flag is not set until after the read takes place. The other thing is it is easier to use std::getline() because it works with std::string s rather than raw char buffers. And you don't need two of everything, you can re-use the std::ifstream if you like.

Also I am not sure if storing the lines in pairs is really what you need? The files are different lengths after all.

Perhaps something more like this will help:

// file names
string file_1="file1.txt";
string file_2="file2.txt";

// vector to store the lines from the files
std::vector<std::string> my_data;

ifstream file;

std::string line; // working variable for input

file.open(file_1.c_str()); // open first file

while(std::getline(file, line)) // while reading one line is successful
       mydata.push_back(line);

file.close();

// now do the same with the second file

file.open(file_2.c_str());

while(std::getline(file, line))
       mydata.push_back(line);

file.close();

That will put all the lines from the first file into the vector and then all the lines from the second file into the vector. The arrangement is different from yours so if its not appropriate just examine how I am reading the lines and use that information for your purpose.

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