- Я сделал программу с помощью которой можно добавлять записи в файл является заказ, но отображение файла после добавления записи в не работает.
Если я добавлю «| ios :: app» в «instream fo», чтобы открыть файл в append , отображение «fin» работает.Отображение содержимого файла в C++
Почему это так? Я с помощью mingw4.8.1
#include<fstream>
#include<iostream>
#include<stdio.h>
class c{
public:
int r;
char nm[20];
};
using namespace std;
int main()
{
c a,b;
ifstream fi("old.txt",ios::binary);
ofstream fo("new.txt",ios::binary); // if |ios::app is added here, the
// display by fin below is working fine
cout<<"Enter roll\t";
cin>>b.r;
cout<<"Enter name\t";
fflush(stdin);
gets(b.nm);
int w=0;
while(true)
{
fi.read((char *)&a,sizeof(a));
if(fi.eof()) break;
if(b.r<a.r&&w==0)
{
fo.write((char *)&b,sizeof(b));
w++;
}
else
fo.write((char *)&a,sizeof(a));
}
ifstream fin("new.txt",ios::binary); // this is not working of the //ios:: is not added in the ofstream fo
while(true)
{
fin.read((char *)&a,sizeof(a));
if(fin.eof()) break;
cout<<a.r<<endl;
puts(a.nm);
}
return 0;
}
Если вы не дают нам [минимальный полный пример] (http://stackoverflow.com/help/mcve), мы должны сделать дополнительные прежде чем мы сможем даже попытаться помочь вам. – Beta