c++ - How to write structure to file using fwrite without unnecessary characters (Null) -
i can't user imputed data, stored in file. tried:
#include<iostream.h> #include<string.h> #include <fstream> struct persons { char name[10]; int dd, mm, yyyy; } a[30],b[30],p; int n,i; int main() { file * pfile; pfile=fopen("data.int","w"); cout<<"number of persons: ";cin>>n; for(i=1;i<=n;i++) { /*cout<<"person: "<<i<<": "<<endl; cout<<"name: "; cin>>a[i].name; cout<<"day (dd): "; cin>>a[i].dd; cout<<"month (mm): "; cin>>a[i].mm; cout<<"year (yyyy): "; cin>>a[i].yyyy; fwrite(&a, sizeof(a),1, pfile);*/ cout<<"persoana: "<<i<<": "<<endl; printf("enter name, day(dd), month(mm) , year(yyyy)\n"); scanf("%s %d %d %d",a[i].name,&a[i].dd,&a[i].mm,&a[i].yyyy); fwrite(&a,sizeof(a),1,pfile); } getchar(); return 0; }
i need in file have persons, on each separated line, in order: name (space) dd (space) mm (space) year (eol). instead have huge line in @ point can see values entered before. had read other topics regarding usage of fwrite, still gives me undesired null values. hints?
many thanks,
Comments
Post a Comment