拆分从文件/ istringstream读取的字符串

时间:2014-10-15 01:37:49

标签: c++ split formatting istringstream

我目前正在参加在线C ++课程,我们当前的项目要求我们阅读一个文件,将每个学生都放到一个载体中。

我目前的问题是将名称分割并将其设置为正确的变量。

教授的伪代码在这里:

inputFile.open(sFileName.c_str ());
while(inputFile.fail())
{
cout ERROR OPENING FILE
cout PLEASE REENTER THE PASSWORD OF THE FILE
getline(cin >> wd, sFileName);
inputFile.open(sFileName.c_str());
}

inputFile.clear();
inputFile.seek(0, ios::beg);

while(getline(inputFile, sTemp))
{
istringstream inputSStream(sTemp)
inputSStream >> sFirstName >> sMiddleName >> sLastName >> sID >> sClass;
if(sMiddleName != "|")
sFullName = sFirst name + " " + sMiddleName +" " + sLastName;
else
sFullName = sFirstName + " " + sLastName;

我目前的代码:

        ifstream myFile;
        string firstName, middleName, lastName, fullName, studentID, cID;
        string inFileName, stringTemp;
        cout << "Please enter the name of the file that you want to read in. \n";
        cin >> inFileName;

        myFile.open(inFileName);
        while (myFile.fail())
        {
            cout << "\n""";
            cout << "Error file, Please re-enter file. \n ";
            cin >> inFileName;
            myFile.open(inFileName);
        }
        myFile.clear();
        myFile.seekg(0, ios::beg);

        while (getline(myFile, stringTemp))
        {
            istringstream inputStream(stringTemp);
            inputStream >> firstName >> middleName >> lastName >> studentID >> cID;
            if (middleName != "|")
                fullName = firstName + " " + middleName + " " + lastName;
            else
                fullName = firstName + " " + lastName; 


            //Checking what the input values are
            cout << "First name is " << firstName << endl;
            cout << "Middle name name is " << middleName << endl;
            cout << "Last name is " << lastName << endl;
            cout << "studentID is " << studentID << endl;
            cout << "CID is " << cID << endl;

            Student thisStudent(fullName, studentID, cID);
            studentList.push_back(thisStudent);

        }



        myFile.close();

目前的问题是线路没有正确分割,名字得到整行,中间和姓氏,studentID和cID都保持为空。

以下是我正在阅读的当前文件:

ERIC,ANTHONY,TURNER,1234573,CISC_198
GABRIEL,FEIJO,LOPES,1234574,CISC_199
GEOFFERY,BRYAN,RANSOM,1234575,CISC_200
HANNAH,MAE ,LONGRIE,1234576,CISC_201
HASSAN,ISMAIL,AHMED,1234577,CISC_202
HUNG,B,PHAM,1234578,CISC_203
HUSSEIN,FOUAD,ALJANABI,1234579,CISC_204
JING,XUN,CHEN,1234580,CISC_205
KAJALBEN,CHIMANLAL,MAKWANA,1234581,CISC_206
DANDREA,SHAMAICAH,BUSH,1234570,CISC_195
DANIELLE,MARIE,CORTEZ,1234571,CISC_196
ERDI,T,KIDANE,1234572,CISC_197
AARON,FABIAN,LINGAD,1234567,CISC_192
AARON,T,PATCHIN,1234568,CISC_193
ALI,FOUAD,ALJANABI,1234569,CISC_194
NATHAN,|,NANN,1234585,CISC_210
NEIL,ANDREAS,FRANKA,1234586,CISC_211
OBONE,|,ORIYAVONG,1234587,CISC_212
OLIVIA,JOANNE,MAILANDER,1234588,CISC_213
RALEIGH,|,COSGROVE,1234589,CISC_214
RYAN,PAREDES,PALMARES,1234590,CISC_215
MICHAEL,DUONG,NGUYEN,1234583,CISC_208
MIGLENA,|,CHEMELEKOVA,1234584,CISC_209
STEPHEN,MICHAEL,HOUSE,1234591,CISC_216
MARCUS,D,BUTLER,1234582,CISC_207

以下是我不明白如何修复的输出。

First name is ERIC,ANTHONY,TURNER,1234573,CISC_198
Middle name name is
Last name is
studentID is
CID is

1 个答案:

答案 0 :(得分:0)

当您使用operator >>阅读时,您正在分割空格。尽管名称如此,getline()可以分隔其他字符而不是换行符:

getline(inputStream, firstName, ',');
getline(inputStream, middleName, ',');
// ...