我的文件正在输出到屏幕两次 C++

my file is outputing to screen twice c++?

本文关键字:两次 C++ 屏幕 文件 输出 我的      更新时间:2023-10-16

大家好,我写了一个代码,我的文件输出了一些单词两次?

帮助

这是类的方法。 我调用 in 主程序。 创建列表没问题,但显示是有问题的。

它显示一些项目两次,主要是最后一个条目的项目。

  void Books ::createList()
  {
  ofstream outfile;
    outfile.open("bookshop.txt",ios::out );

   cout << "enter the size of list" << endl;
   cin >> n;
   if (n <= 0)
   {
    cout << "invalid entry or list empty" << endl;
   }///end if.
  else
   {
    i = 1 ;
    if (i > n)
        tmp1Ptr->next = NULL;
    else
        cout << "enter value for the list" << endl;
    for(i=1; i<=n ; i++)
    {
        cin.ignore();
        cout << "enter the author" << endl;
        getline(cin , authr);
        cout <<endl << "enter the title" << endl;
        getline(cin , titl);
        cout << endl << "enter the publisher" << endl;
        getline(cin , publishr);
        cout << endl<< "enter the price" << endl;
        cin >> pric;
        cout << endl << "enter the stock position" << endl;
        cin >> stoc;
        tmp2Ptr = new Book;
        tmp2Ptr->author = authr ;
        tmp2Ptr->title = titl ;
        tmp2Ptr->publisher = publishr ;
        tmp2Ptr->price = pric ;
        tmp2Ptr->stock = stoc ;
        outfile<<tmp2Ptr->author << endl;
        outfile<<tmp2Ptr->title <<endl;
        outfile<<tmp2Ptr->publisher <<endl;
        outfile<<tmp2Ptr->price <<endl;
        outfile<<tmp2Ptr->stock <<endl;
        if (i == 1)
        {
            headPtr = tmp2Ptr;
            tmp1Ptr = headPtr ;
            tmp1Ptr->prev = NULL;
        }///end inner if
        else
        {
            tmp1Ptr->next = tmp2Ptr;
            tmp2Ptr->prev = tmp1Ptr;
            tmp1Ptr = tmp2Ptr;

        }///end inner else.

    }///end for
    tmp1Ptr->next = NULL;
    tmp1Ptr = NULL;
    tmp2Ptr = NULL;
    cout << endl;
 }
 outfile.close();
 }///end createList
  void Books ::displayDetails()
   {
   ifstream infile;
   infile.open("bookshop.txt", ios::in);

  cout << "contents of list are:" << endl;
               while (infile.good())
               {
                string priz;
               string stok;

               getline(infile,authr1);
               getline(infile,titl1);
               getline(infile,publishr1);
               getline(infile,priz);
               getline(infile,stok);


               cout<< authr1 << endl;
                cout<< titl1 << endl;
                cout<< publishr1 << endl;
                cout << priz << endl << stok << endl;

               }


               infile.close();
                }

不要做while (infile.good())(或while (!infile.eof())),因为它不会按预期工作(正如你注意到的)。相反,例如 while (getline(...)) .

问题是,在您尝试从文件末尾读取之前,不会设置 eofbit 标志,因为在您实际到达文件并尝试超越文件之前,没有可靠的方法来检测文件末尾。这会导致像 while (infile.good()) 这样的循环迭代一次到多次,然后循环内的所有getline调用都将失败(第一次调用设置 eofbit 标志),但由于您没有在循环中检查这一点,您将打印出无效数据。

而是依赖于以下事实:std::getline返回流对象(或者更确切地说是对它的引用),并且流可以在布尔表达式中使用。因此,如果getline调用失败,则当getline返回时,将在流中设置正确的标志,并且在停止循环的情况下使用时,流的计算结果将为false

尝试在循环中使用它:

*while (infile >> authr1 >> titl1 >> publishr1 >> priz >> stok)
  cout<< authr1  << titl1  << publishr1  << priz << stok << endl;*

在您实际阅读 eof 之前,eof 不会到达,因此 while(infile.good()) 将不起作用。