我的文件正在输出到屏幕两次 C++
my file is outputing to screen twice c++?
大家好,我写了一个代码,我的文件输出了一些单词两次?
帮助这是类的方法。 我调用 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()) 将不起作用。
相关文章:
- g++的分段错误(在NaN上使用to_string两次时)
- 蛇在C++不会连续转两次
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 我应该如何去缓解两次出现的cin?
- Realloc 两次无法在 Visual Studio 上运行
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- C++析构函数调用两次,堆栈分配的复合对象
- 为什么参数在构造 std::thread 时移动两次
- Qt插槽调用了两次
- 做 std::用相同的unique_ptr移动两次
- C++两次定义相同的函数会导致错误
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 对于优化级别为 0 的 std::vector,析构函数被调用两次
- 使用柯南打包时如何避免列出两次依赖?
- 为什么要执行两次位移((x >> 4)<< 4)?
- 对结构向量进行两次排序
- 如果我使用同一个密钥推送用户数据两次,会发生什么
- 谷歌测试发现在macOSX上添加并运行了我的所有测试两次