Tôi đang cố gắng đọc từng dòng của một tệp văn bản mà mỗi dòng chứa một từ và đặt các từ đó thành một vectơ. Làm thế nào tôi sẽ đi về làm điều đó?Đọc dòng từ tệp văn bản và đặt các chuỗi vào một vectơ?
Đây là mã mới của tôi: Tôi nghĩ rằng vẫn còn điều gì đó sai trái với nó.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
std::string line;
vector<string> DataArray;
vector<string> QueryArray;
ifstream myfile("OHenry.txt");
ifstream qfile("queries.txt");
if(!myfile) //Always test the file open.
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while (std::getline(qfile, line))
{
QueryArray.push_back(line);
}
if(!qfile) //Always test the file open.
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while (std::getline(qfile, line))
{
QueryArray.push_back(line);
}
cout<<QueryArray[0]<<endl;
cout<<DataArray[0]<<endl;
}
Bạn có vấn đề gì với mã cho đến nay? – Mahesh
@Mahesh this * if (! Myfile) * có thể là vấn đề đầu tiên. (Tôi xin lỗi .. cần phải học STL.) – Beginner
@RomanB: Không có gì sai với dòng đó. – Puppy