将逗号分隔的文本文件读入数组

时间:2016-04-25 19:54:41

标签: c++ arrays readfile getline

我正在尝试用C ++编写一个模拟大学注册系统的程序,学生输入他们的ID,程序在文本文件中搜索他们的信息,并根据文本文件加载结构。我已经达到了一个难以将他们的注册课程纳入struct数组的地步。使用getline函数,使用','作为delim也将继续下一行直到下一个逗号。什么是正确的算法?

这是包含学生信息的文件设置:

  • 918273645,史蒂夫,奥尔布赖特,ITCS2530,MATH210,ENG140
  • 123456789,Kim,Murphy,ITCS2530,MATH101
  • 213456789,迪安,鲍尔斯,ITCS2530,ENG140
  • 219834765,杰里,克拉克,MGMT201,MATH210

(为布局添加项目符号;不在文件中添加)

例如,用户输入“123456789”作为其ID,然后读取Kim Murphy的信息。在getline的第一次迭代时,读取“ITCS2530”并将其放入变量中,然后将其加载到结构中;没问题。但是,列表中的最后一个过程在下一个逗号之前有换行符,因此下一个迭代读取“MATH101 / nl213456789”并将整个字符串放入变量并尝试将其加载到结构中。

第一列是他们的ID,然后是他们的名字,姓氏,然后是他们当前注册的课程。请注意,注册课程的数量可能会有所不同。

以下是我目前正在处理的代码:

    student login()
{
    string ID;
    student newStudent;
    string enrolled;
    int i = 0;
    while (true)
    {
        cout << "Enter Student ID: ";
        cin >> newStudent.ID;
        cout << endl;
        if (newStudent.ID.length() == 9)
            break;
        else
            cout << "That ID is invalid - IDs are 9 digits" << endl;
    }
    ifstream inFile;
    ofstream outFile;
    inFile.open("registration.txt");
    if (inFile.is_open())                                                               
    //Check if file is open
    {
        while (!inFile.eof())                                                           
        //While not at end of file
        {
            getline(inFile, ID, ',');                                                   
            //Search for ID
            if (ID == newStudent.ID)
            {
                getline(inFile, newStudent.fName, ',');                                         
                //Assign fName and lName
                getline(inFile, newStudent.lName, ','); 

                while (enrolled != "\n")
                {
                    getline(inFile, enrolled, ',');
                    if (enrolled == "\n")
                    {
                        cout << "Not currently enrolled in a class." << endl;
                    }
                    else
                    {
                        newStudent.courses[i] = enrolled;
                        i++;
                    }
                }
                    cout << newStudent.lName << ", Welcome to the MCC Enrollment System!" << endl;
                for (i = 0; i <= NUM_OF_COURSES; i++)
                {
                    cout << "Enrolled courses: " << newStudent.courses[i] << endl;
                }

                    cout << endl;
                    break;
                    //Stops searching
            }
            else                                                                        
                //Ignores rest of line - used to skip to the next line
            {
                getline(inFile, ID, '\n');
            }
            if (inFile.eof())                                                           
                //If ID was not found
            {
                inFile.close();
                cout << "Enter First Name: ";                                           
                //Begin entry of new student
                cin >> newStudent.fName;
                cout << endl;
                cout << "Enter Last Name: ";
                cin >> newStudent.lName;

                cout << endl;
                outFile.open("registration.txt", ios::app);
                if (outFile.is_open())
                {
                    outFile << newStudent.ID << "," << newStudent.fName << "," << newStudent.lName << "\n";
                }
            }
        }
    }
    return newStudent;
}

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

问题是std :: getline只需要一个字符作为分隔符。它默认为换行符,但如果你使用另一个字符,则换行符不再是分隔符,因此你最终会在文本中添加换行符。

答案是使用带有默认(换行符)分隔符的std :: getline将整行读入字符串,然后使用字符串流来保存该行文本,以便您可以使用逗号调用std :: getline分隔符。

这样的事情:

#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>

int main()
{
    std::ifstream inFile("registration.txt");
    if (inFile.is_open())
    {
        std::string line;
        while( std::getline(inFile,line) )
        {
            std::stringstream ss(line);

            std::string ID, fname, lname;
            std::getline(ss,ID,',');    std::cout<<"\""<<ID<<"\"";
            std::getline(ss,fname,','); std::cout<<", \""<<fname<<"\"";
            std::getline(ss,lname,','); std::cout<<", \""<<lname<<"\"";

            std::vector<std::string> enrolled;
            std::string course;
            while( std::getline(ss,course,',') )
            {
                 enrolled.push_back(course); std::cout<<", \""<<course<<"\"";
            }
            std::cout<<"\n";
        }
    }
    return 0;
}

在这个例子中,我将文本写入由引号括起的屏幕,以便您可以看到所读取的内容。

答案 1 :(得分:0)

split(string, seperator)

split("918273645,Steve,Albright,ITCS2530,MATH210,ENG140", ",")
split("123456789,Kim,Murphy,ITCS2530,MATH101", ",")
split("213456789,Dean,Bowers,ITCS2530,ENG140", ",")
split("219834765,Jerry,Clark,MGMT201,MATH210", ",")

我对C ++知之甚少,但我记得这个命令。