R
Rafael Anschau
Hi, the following program uses some c++, so I hope it´s not off topic.
It reads a structure into a file 3 times, but when I tell it to print
them all, it prints the last one twice. I appreciate any
help on what am I doing to cause this.
Thank you,
Rafael
#include <string.h>
#include <string>
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char nf[]="1.tst";
string str="Hello world";
FILE *fp;
if ((fp=fopen(nf,"w+"))==NULL) {
std::cout<<"Erro nao abriu";
int a;
cin>>a;
exit(1);
}
typedef struct endereco {
string rua;
int n;
} ender;
ender meu,teu;
int i;
rewind(fp);
for(i=0;i<3;i++) {
cout<<"Nome da rua:\n";
cin>>meu.rua;
cout<<"Numero do ap:\n";
cin>>meu.n;
fwrite(&meu,sizeof(ender),1,fp);
}
cout<<"Listando\n";
rewind(fp);
while(!feof(fp)) {
fread(&teu,sizeof(ender),1,fp);
cout<<teu.rua<<"\n";
cout<<teu.n<<"\n";
}
int a;
cin>>a;
It reads a structure into a file 3 times, but when I tell it to print
them all, it prints the last one twice. I appreciate any
help on what am I doing to cause this.
Thank you,
Rafael
#include <string.h>
#include <string>
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char nf[]="1.tst";
string str="Hello world";
FILE *fp;
if ((fp=fopen(nf,"w+"))==NULL) {
std::cout<<"Erro nao abriu";
int a;
cin>>a;
exit(1);
}
typedef struct endereco {
string rua;
int n;
} ender;
ender meu,teu;
int i;
rewind(fp);
for(i=0;i<3;i++) {
cout<<"Nome da rua:\n";
cin>>meu.rua;
cout<<"Numero do ap:\n";
cin>>meu.n;
fwrite(&meu,sizeof(ender),1,fp);
}
cout<<"Listando\n";
rewind(fp);
while(!feof(fp)) {
fread(&teu,sizeof(ender),1,fp);
cout<<teu.rua<<"\n";
cout<<teu.n<<"\n";
}
int a;
cin>>a;