簡體   English   中英

將逗號分隔的文本文件讀入數組

[英]Reading Comma Delimited Text File Into Array

我正在嘗試用C ++編寫一個程序,以模擬大學的錄取系統,學生在該系統中輸入其ID,然后該程序在文本文件中搜索其信息,並基於該文本文件加載結構。 我已經到了難以將他們的已注冊課程納入結構數組的地步。 使用getline函數,使用','作為delim,也將延續到下一行,直到下一個逗號為止。 正確的算法是什么?

這是包含偽造學生信息的文件設置:

  • 918273645,史蒂夫,奧爾布賴特,ITCS2530,MATH210,ENG140
  • 123456789,金,墨菲,ITCS2530,MATH101
  • 213456789,Dean,Bowers,ITCS2530,ENG140
  • 219834765,Jerry,Clark,MGMT201,MATH210

(已添加子彈以進行布局;不在文件中)

例如,用戶輸入“ 123456789”作為其ID,然后讀取Kim Murphy的信息。 在getline的第一次迭代中,將讀取“ ITCS2530”並將其放入變量中,然后將其加載到struct中。 沒問題。 但是,列表中的最后一個課程在下一個逗號之前具有換行符,因此下一次迭代將讀取“ 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;
}

在此先感謝您的幫助。

問題是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;
}

在此示例中,我將文本寫在屏幕上並用引號引起來,以便您可以看到所讀內容。

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 ++的了解很少,但是我記得這個命令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM