C++ C++;将文件读入向量
我很难理解如何逐行将文件读入不同的数据类型向量。有没有办法用infle>>做到这一点?我的代码如下。提前谢谢C++ C++;将文件读入向量,c++,file,vector,C++,File,Vector,我很难理解如何逐行将文件读入不同的数据类型向量。有没有办法用infle>>做到这一点?我的代码如下。提前谢谢 void fileOpen() { fstream inFile; inFile.open(userFile); // Check if file open successful -- if so, process if (!inFile.is_open()) {cout << "File could not be opened.";}
void fileOpen()
{
fstream inFile;
inFile.open(userFile);
// Check if file open successful -- if so, process
if (!inFile.is_open()) {cout << "File could not be opened.";}
else
{
cout << "File is open.\n";
string firstLine;
string line;
vector<char> printMethod;
vector<string> shirtColor;
vector<string> orderID;
vector<string> region;
vector<int> charCount;
vector<int> numMedium;
vector<int> numLarge;
vector<int> numXL;
getline(inFile, firstLine); // get column headings out of the way
cout << firstLine << endl << endl;
while(inFile.good()) // while we are not at the end of the file, process
{
while(getline(inFile, line)) // get each line of the file separately
{
for (int i = 1; i < 50; i++)
{
inFile >> date >> printMethod.at(i);
cout << date << printMethod.at(i) << endl;
}
}
}
}
}
void fileOpen()
{
河道充填;
打开(用户文件);
//检查文件打开是否成功——如果成功,则处理
如果(!infle.is_open()){cout在使用vector.at(i)之前
在你的情况下,你应该确保你的向量足够长,因为它会产生超出范围的异常。正如我从你的代码中看到的,你的向量printMethod
包含的元素不超过50个,因此你可以在使用前调整向量printMethod
vector<char> printMethod(50);
您应该使用push_back
将项目添加到向量
。我认为您无法在成员函数中获得索引超出范围的。日期变量没有定义
vector<char> printMethod;
printMethod.resize(50);
char other_data;
inFile >> date >> other_data;
printMethod.push_back(other_data);