文件i / o任务跟踪器问题

时间:2014-04-20 23:59:46

标签: c++ file input io c-strings

我一直在努力创建一个能够执行三种不同功能的任务跟踪控制台应用程序 - >添加任务,显示任务列表,按类名搜索任务列表。我添加任务的代码工作正常,但我试图弄清楚如何正确显示任务列表,但它仍然只输出部分任务。

例如,如果您添加任务CS162; Lab Due; 2014年4月20日和任务 MTH251;测验; 2014年4月22日。如果你关闭文件并查看它,它们就被写入tasks.txt了,但是当我去读它们时,输出看起来像:  Due04 /二千零十四分之二十零 MTH251Quiz 这只是所需输出的一小部分。 我还需要按姓名搜索,但我想在找出其他问题后我可以很容易地解决这个问题

我的文本文件如下所示:

CS162;Lab Due;04/20/2014
MTH251;Quiz;04/22/2014

我的代码如下:

#include <iostream>
#include <fstream>
#include <iomanip>


using namespace std;

ofstream outFile;
ifstream inFile;

const int MAXCHAR = 101;
const int MAXLINE = 256;

struct task
{
    char course[MAXCHAR];
    char desc[MAXCHAR];
    char date[MAXCHAR];
};

int main()
{
    task track;
    bool quit = false;
    while (quit == false)
    {
        char choice;
        cout << "Welcome to my Task List: \n";
        cout << "<a> to add task\n";
        cout << "<s> to show the task list\n";
        cout << "<f> to find a task by course name\n";
        cout << "<q> to quit\n";
        cin >> choice;
        cin.ignore(100, '\n');
        if (choice == 'a' || choice == 'A')
        {
            outFile.open("tasks.txt", fstream::app);
            cout << "Enter Course Name (less than 101 characters): ";
            cin.get(track.course, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            cout << "Enter Task Description (less than 101 characters): ";
            cin.get(track.desc, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            cout << "Enter due date (mm/dd/yyyy): ";
            cin.get(track.date, MAXCHAR, '\n');
            cin.clear();
            cin.ignore(100, '\n');

            char confirm;
            cout << "\nAre you sure you want to add " << track.course << ";" << track.desc << ";" << track.date << "? (y/n)";
            cin >> confirm;
            if (confirm == 'y' || confirm == 'Y')
            {
                cin.clear();
                cin.ignore(100, '\n');
                outFile << track.course << ";" << track.desc << ";" << track.date << "\n";
                cout << "Task has been added\n";
            }
            else if (confirm == 'n' || confirm == 'N')
            {
                cin.clear();
                cin.ignore(100, '\n');
            }
            outFile.close();
        }
        else if (choice == 's' || choice == 'S')
        {
            inFile.open("tasks.txt");
            char str[MAXCHAR];
            while (inFile >> str)
            {


                inFile.getline(track.course, MAXLINE, ';');
                inFile.getline(track.desc, MAXLINE, ';');
                inFile.getline(track.date, MAXLINE, ';');
                cout << track.course << track.desc << track.date;


            }

            inFile.close();
            cin.clear();
            cin.ignore(100, '\n');

        }

        else if (choice == 'f' || choice == 'F')
        {
            char course[MAXCHAR];
            cout << "Enter Course Name: ";
            cin >> course;

            inFile.open("tasks.txt");
            while (!inFile)
            {



            }
            inFile.close();

        }
        else if (choice == 'q' || choice == 'Q')
        {
            cin.clear();
            cin.ignore(100, '\n');
            quit = true;
        }

    }

}

编辑: 我现在有“显示任务列表”正常工作,但有人可以向我解释为什么使用这样的东西不适用于“搜索课程”??

else if (choice == 'f' || choice == 'F')
    {
    char course[MAXCHAR];
    cout << "Enter Course Name: ";
    cin >> course;

    inFile.open("tasks.txt");
    while (inFile)
    {
    inFile.getline(track.course, MAXLINE, ';');
    inFile.ignore(100, '\n');
    while (strcmp(course, track.course) != 0)
            {
        cout << track.course;

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

while (inFile >> str) {
  inFile.getline(track.course, MAXLINE, ';');
  inFile.getline(track.desc, MAXLINE, ';');
  inFile.getline(track.date, MAXLINE, ';');
  std::cout << track.course << track.desc << track.date;
}

第一行会将所有字符读入str,直到它到达第一个空格,所以如果输入是

CS162;Lab Due;04/20/2014
MTH251;Quiz;04/22/2014

它会将Lab读入并包含str

接下来的三行将一直读到;,所以你的音轨结构将如下所示

course = "Due"
desc   = "04/20/2014\nMTH251" // note the newline
date   = "Quiz"

更改while循环并更改最后一个提取器应解决此问题:

while (inFile) {
  inFile.getline(track.course, MAXLINE, ';');
  inFile.getline(track.desc, MAXLINE, ';');
  inFile.getline(track.date, MAXLINE, '\n');
  if (inFile)
    std::cout << track.course << track.desc << track.date;
}

虽然值得一提的是,您可以在此处执行许多操作来简化代码,包括使用std::string代替char数组,like this for instance。< / p>