c++ - 我的文件输出到屏幕两次 C++?

标签 c++ file data-structures

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

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

它显示了一些项目两次,大部分是最后一个条目的项目。

  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 标志不会被设置,直到您尝试从文件末尾读取 beyond 之后,因为没有可靠的方法来检测文件末尾,直到你真正到达它并尝试超越它。这会导致像 while (infile.good()) 这样的循环迭代一次到多次,然后循环内的所有 getline 调用都会失败(第一次调用设置eofbit 标志),但由于您不在循环内检查它,您将打印出无效数据。

而是依赖于 std::getline 的事实返回流对象(或者更确切地说是对它的引用)并且流可以 be used in a boolean expression .因此,如果 getline 调用失败,则当 getline 返回时,将在流中设置正确的标志,并且流将评估为 false用于停止循环的条件。

关于c++ - 我的文件输出到屏幕两次 C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35697097/

相关文章:

java - 通过socket编程访问远程目录

java - Google Kickstart : Round C, 2020:稳定墙 - 回溯解决方案错误

c - 即使对于大量数据作为输入,我如何使该代码也能工作?

c++ - CL.exe 在 Visual Studio 2012 中崩溃

c++ - 比较 C++ 中错误的数值表示中的字符

ruby - ruby 中的 unshift + file.join

ios - 适用于 iOS 的文件查看器

c# - 应该使用 : array vs linked list?

c++ - 使用 C++ 十六进制和 cin

c++ - POD 对包含标准库容器的结构的影响