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



我一直在尝试制作一个任务跟踪器控制台应用程序,该应用程序能够执行三种不同的功能-->添加任务、显示任务列表和按类名搜索任务列表。我添加任务的代码运行良好,但我试图找出如何正确显示任务列表,但它只输出部分任务。

例如,如果您添加任务CS162;实验室到期;2014年4月20日和任务MTH251;测验2014年4月22日。如果你关闭文件并查看它,它们被写入tasks.txt很好,但当我去读回它们时,输出看起来像:2014年4月20日到期MTH251测验这仅仅是期望输出的一小部分。我还需要按名字搜索,但我想在我找到其他问题后,我可以很容易地找到

我的文本文件如下:

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 taskn";
        cout << "<s> to show the task listn";
        cout << "<f> to find a task by course namen";
        cout << "<q> to quitn";
        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 addedn";
            }
            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;
            }
        }
    }
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

则它将向上读取CCD_ 2并将其包括到CCD_。

接下来的三行将读取,直到它们到达;,因此您的轨道结构将如下所示

course = "Due"
desc   = "04/20/2014nMTH251" // 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数组,例如这样。

相关内容

  • 没有找到相关文章

最新更新