Ofstream 不会写入所有内容 — 截断的数据
ofstream doesn't write everything—truncated data
在代码的最后一部分,我的ofstream变量出现了问题。如何避免丢失流中的信息?问题是由程序的最后一部分生成的。
以下是我得到的:
Action Movies
Comedy Movies
Drama Movies
Horror Movies
There are no movies for this genre
Mystery Movies
There are no movies for this genre
Romantic Movies
Adventure Movies
Western Movies
ife 1946 D 130 30 8.6 1946 103101
The Best Years of Our Lives 1946 R 172 181 8.1 1946 17648
Lawrence of Arabia 1962 V 216 48 8.5 1962 80746
For a Few Dollars More 1965 W 132 123 8.2 1965 45515
这是我应该得到的:
Action Movies
Braveheart 1995 A 177 88 8.3 1995 245089
Batman begins 2005 A 140 110 8.3 2005 275523
Comedy Movies
Amelie 2001 C 122 45 8.5 2001 185124
Singin' in the Rain 1952 C 103 77 8.3 1952 56368
Drama Movies
it's a Wonderful Life 1946 D 130 30 8.6 1946 103101
Horror Movies
There are no movies for this genre
Mystery Movies
There are no movies for this genre
Romantic Movies
The Best Years of Our Lives 1946 R 172 181 8.1 1946 17648
Adventure Movies
Lawrence of Arabia 1962 V 216 48 8.5 1962 80746
Western Movies
For a Few Dollars More 1965 W 132 123 8.2 1965 45515
这是我的打印功能:错误必须在这里,但我不知道如何修复
void movieType::printMovieInfo(char* outFileName)
{
std::ofstream outFile;
if(!outFile.is_open())
outFile.open(outFileName, std::ios::app);
outFile<<name<<" "<<year<<" "<<genre<<" "<<length<<" "<<rank;
outFile<<" "<<weight<<" "<<year<<" "<<votes<<std::endl;
}
编辑至:
void movieType::printMovieInfo(std::ofstream& outFile)
{
outFile<<name<<" "<<year<<" "<<genre<<" "<<length<<" "<<rank;
outFile<<" "<<weight<<" "<<year<<" "<<votes<<std::endl;
}
在主体的最后部分:
ij.printMovieInfo("printList.txt");
更改为
ij.printMovieInfo(outFile);
这是我的主要功能:问题是在程序结束时生成的
#include "movieType.h"
#include <iostream>
#include <fstream>
#include <string>
#include "PQType.h"
#include "QueType.h"//FIFO queue
using namespace std;
int main()
{
movieType movie[9];
ifstream inFile("movie1.txt");
ofstream outFile("printList.txt");
int i =0;
bool notDone=true;
while (notDone)
{
if (movie[i++].readMovieInfo(inFile)== false)
notDone=false;
}
PQType<movieType> PQqueue(8);
int j=0;
while(!PQqueue.IsFull())
{
PQqueue.Enqueue(movie[j++]);
}
QueType<movieType> fifoQueue[8];
string Genre[8]={"Action", "Comedy", "Drama", "Horror", "Mystery", "Romantic", "Adventure","Western"};
movieType it;
while (!PQqueue.IsEmpty())
{
PQqueue.Dequeue(it);
if (it.getGenre() == 'A')
fifoQueue[0].Enqueue(it);
else if (it.getGenre() == 'C' )
fifoQueue[1].Enqueue(it);
else if (it.getGenre() == 'D')
fifoQueue[2].Enqueue(it);
else if (it.getGenre() == 'H')
fifoQueue[3].Enqueue(it);
else if (it.getGenre() == 'M')
fifoQueue[4].Enqueue(it);
else if (it.getGenre() == 'R')
fifoQueue[5].Enqueue(it);
else if (it.getGenre() == 'V')
fifoQueue[6].Enqueue(it);
else if (it.getGenre() == 'W')
fifoQueue[7].Enqueue(it);
}
//Problem is generated here.
movieType ij;
for (int i=0;i<8;++i)
{
outFile<<Genre[i]<<" Movies"<<endl;
if (fifoQueue[i].IsEmpty())
outFile<<"There are no movies for this genre"<<endl;
for(int j=0; fifoQueue[i].IsEmpty() != true; ++j)
{
fifoQueue[i].Dequeue(ij);
ij.printMovieInfo("printList.txt");
}
outFile<<endl;
}
return 0;
}
通过快速阅读您的代码,我注意到您正在打开文件进行两次写入。第一次在主函数的开头,第二次在printMovieInfo函数中。
您真的不应该打开一个文件进行两次写入,因为这两个流可能位于不同的位置。
您可以做的是将主函数中的ofstream传递给printMovieInfo函数作为引用。希望这能解决问题。
通过快速地进一步浏览代码,您还可以在调用printMovieInfo之前清除流。不过,我还是推荐第一种选择。
否则,正如Roberto所问,您可能无法从输入文件中获取所有数据。
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据不是用 ofstream 写入的,即使返回成功也是如此
- Ofstream 不会写入所有内容 — 截断的数据
- c++可以使用fstream-infile从最近使用的ofstream-outfile中收集数据
- 在使用 ofstream 将数据写入其文件后更新 ifstream 对象
- 在c++中使用ofstream打开文件来编辑以前的数据
- 在 C++ 中使用 ofstream 将 32 位二进制数据写入文件
- 使用ifstream和ofstream序列化二进制数据时出现问题