Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Printing the last item of a structure twice
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Rafael Anschau, post: 3910428"] 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; [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Printing the last item of a structure twice
Top