【发布时间】:2023-03-22 13:19:01
【问题描述】:
我正在尝试读取多个 .txt 文件并将每个文本中的每一行 push_back 到字符串类型的向量。 因此:第一个文件有 200 行。 第二个文件有 800 行。
但是,我在读取第二个文件之前遇到了问题。
#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;
}
感谢您抽出宝贵时间帮助我。
【问题讨论】: