Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;将文件读入向量_C++_File_Vector - Fatal编程技术网

C++ C++;将文件读入向量

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.";}

我很难理解如何逐行将文件读入不同的数据类型向量。有没有办法用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.";}
    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);