基本C++程序,getline()/parseing a file

Basic C++ program, getline()/parsing a file

本文关键字:parseing file C++ 程序 getline 基本      更新时间:2023-10-16

我的任务是创建一个小程序,用于解析文本文件并从中获取必要的信息。文件的布局是这样的

Tuesday*Info5051*10:00*11:00*M3039*Info5064*12:00*3:00*G1001;
基本上,它

应该将每个字符串存储在结构中,以便我以后可以检索它,但我无法让我的程序工作(我有学习障碍,所以事情往往会变得困难)。这是我到目前为止的代码。(我知道这是一个简单的程序,但我倾向于过度思考/搞砸东西。到目前为止,我遇到的大问题是它无法打开文件以启动。我已将文件保存到bin->debug以及程序的主文件夹中。我确定我错误地使用了 getline 方法。

struct Course
{
    string _sDay;
    string _sName;
    string _sCode;
    string _iStart;
    string _iDuration;
    string _sRoom;
};
int main()
{
    ifstream fileIn;
    fileIn.open("courseLoad.txt");
    vector<Course> vCourse;
    string str="*";
    string line;
    if (!fileIn)
    {
        cout<<"A error has occured, please contact support.";
    }
    while(!fileIn.eof())
    {
        for(int i=0; i!= fileIn.eof();i++)
        {
            //file.getline(entry.part_num, 6, '-');
            getline(fileIn,line,'*');
            vCourse[i]._sDay =line;
            getline(fileIn,line,'*');
            vCourse[i]._sName =line;
            getline(fileIn,line,'*');
            vCourse[i]._sCode = line;
            getline(fileIn,line,'*');
            vCourse[i]._iStart =line;
            getline(fileIn,line,'*');
            vCourse[i]._iDuration = line;
            getline(fileIn,line,'*');
            vCourse[i]._sRoom =line;
            cout<<vCourse[i];
        }//end for
    }
--output to screen here--

此代码有几个问题:

1) 该代码缺少 return 语句或 else 语句,以防止程序在无法打开文件的情况下继续执行:

if (!fileIn)
{
    cout<<"A error has occured, please contact support.";
    return -1;
}

2)您的获取线都在同一个输入流上运行。您想在一行中读取,然后解析该行。例如:

// Read in a line
while (getline(fileIn,line))
{
    string item;
    std::stringstream sstr(line);
    // Read in an item
    while (getline(sstr, item, "*"))
    {
        std::cout << item << std::endl;
    }
}

3) vCourse 大小为 0,因此不能使用 [] 运算符;但是您可以使用push_back来扩展矢量的大小并在矢量的后面插入一个元素:

// Read in a line
while (getline(fileIn,line))
{
    string item;
    // Default course construction
    Course c;
    std::stringstream sstr(line);
    // Read in an item
    getline(sstr,item,'*');
    c._sDay = item;
    getline(sstr,item,'*');
    c._sName = item;
    getline(sstr,item,'*');
    c._sCode = item;
    getline(sstr,item,'*');
    c._iStart = item;
    getline(sstr,item,'*');
    c._iDuration = item;
    getline(sstr,item,'*');
    c._sRoom = item;
    // Save the course into the vector
    vCourse.push_back(c);
}

您还可以在上面添加更多的错误检查(以防行中缺少某些元素)。

一个显而易见的直接问题是,您实际上并没有将任何Course结构添加到向量中,而是像向量一样分配给它们的元素。例如

    vCourse[i]._sDay =line;

但是您实际上并没有在索引 i 处向量添加 Course 结构的缩存。这意味着您分配给不存在的实例,这绝不是好消息。在此之前,您需要的是

    Course newItem;             // make a new Course object instance
    vCourse.push_back(newItem); // This adds the instance to the end of the vector
    // Now assign to the members of vCourse[i];
    vCourse[i]._sDay =line;
    getline(fileIn,line,'*');
    vCourse[i]._sName =line;
    getline(fileIn,line,'*');
    vCourse[i]._sCode = line;
    getline(fileIn,line,'*');
    vCourse[i]._iStart =line;
    getline(fileIn,line,'*');
    vCourse[i]._iDuration = line;
    getline(fileIn,line,'*');

然后,您可以分配给结构。

另外,如果您想这样做

    cout<<vCourse[i];

您将需要重载operator<<

如果您无法打开文件,则需要检查您是否 1) 正确拼写了文件名,2) 该文件是否与可执行文件位于同一位置。无论如何写完整路径名可能会更安全

你也可以尝试将文件的内容放入单个字符串并使用strtok()函数。