I read the records from the file in the array here, in case 3 I edit the record by the entered last name, I wrote down the array, now how to write everything correctly in the file having these variables?

//--------------------------------------------------------------------------- #pragma hdrstop //--------------------------------------------------------------------------- #include <vcl.h> #pragma argsused //--------------------------------------------------------------------------- #include <stdio.h> #include <io.h> #include <conio.h> //--------------------------------------------------------------------------- struct TZap { char FIO[30]; int god_r; int n_gr; int ocen_fiz, ocen_mat, ocen_inf, ocen_him; double s_b; } Zap; int size = sizeof( TZap ); FILE *Fz, *Ft; char File_Zap[] = "zapisi.dat"; char File_Rez[] = "rezult.txt"; void Out( TZap ); int main( int argc, char *argv[] ) { int kod, D_f, i = 0, j, kol, x = -1; char afio[30]; int agod_r, an_gr, aocen_fiz, aocen_mat, aocen_inf, aocen_him, as_b, kod_edit; long len; TZap st, *mas_Z; Ft = fopen( File_Rez, "w" ); char fio_edit[30]; while( true ) { puts( "\n\n 1 - Create\n 2 - Add\n 3 - Edit\n 4 - View\n\n 0 - Exit" ); printf( "\n\t" ); scanf( "%d", &kod ); switch( kod ) { case 1: if( ( Fz = fopen( File_Zap, "wb" ) ) == NULL ) { puts( "\n Create ERROR! Press any key!" ); getch(); continue; } fclose( Fz ); printf( "\n Create New File %s !\n", File_Zap ); break; case 2: Fz = fopen( File_Zap, "ab" ); printf( "\n FIO - " ); fflush( stdin ); printf( "\n\t" ); gets( Zap.FIO ); printf( " Year of birth - " ); printf( "\n\t" ); scanf( "%d", &Zap.god_r ); printf( " Group number - " ); printf( "\n\t" ); scanf( "%d", &Zap.n_gr ); printf( " Physics - " ); printf( "\n\t" ); scanf( "%d", &Zap.ocen_fiz ); printf( " Mathematics - " ); printf( "\n\t" ); scanf( "%d", &Zap.ocen_mat ); printf( " Computer science - " ); printf( "\n\t" ); scanf( "%d", &Zap.ocen_inf ); printf( " Chemistry - " ); printf( "\n\t" ); scanf( "%d", &Zap.ocen_him ); printf( " Mark - " ); printf( "\n\t" ); scanf( "%lf", &Zap.s_b ); fwrite( &Zap, size, 1, Fz ); fclose( Fz ); break; case 3: if( ( Fz = fopen( File_Zap, "rb" ) ) == NULL ) { puts( "\n Open ERROR! Press any key!" ); getch(); continue; } printf( "\n Vvedite FIO" ); fflush( stdin ); printf( "\n\t" ); gets( fio_edit ); Fz = fopen( File_Zap, "rb" ); D_f = fileno( Fz ); len = filelength( D_f ); kol = len / size; mas_Z = new TZap[kol]; for( i = 0; i < kol; i++ ) { fread( ( mas_Z + i ), size, 1, Fz ); } fclose( Fz ); for( int i = 0; i < kol; ++i ) if( strcmp( mas_Z[i].FIO, fio_edit ) == 0 ) { x = i; break; } else { x = -1; } if( x == -1 ) { puts( "\tSovpadenii net" ); } else { printf( " Edit:\n 1 - FIO\n 2 - Year of birth\n 3 - Group number\n 4 - Physics\n 5 - Mathematics\n 6 - Computer science\n 7 - Chemistry\n 8 - Mark\n" ); printf( "\n\t" ); scanf( "%d", &kod_edit ); if( kod_edit == 1 ) { printf( " Vvedite FIO" ); printf( "\n\t" ); scanf( "%s", &afio ); strcpy( mas_Z[x].FIO, afio ); printf( " %s", mas_Z[x].FIO ); } } delete []mas_Z; break; case 4: if( ( Fz = fopen( File_Zap, "rb" ) ) == NULL ) { puts( "\n Open ERROR! Press any key!" ); getch(); continue; } printf( "\n\t--------- Informations ---------\n" ); fprintf( Ft, "\n\t--------- Informations ---------" ); while( 1 ) { if( !fread( &Zap, size, 1, Fz ) ) { break; } Out( Zap ); } fclose( Fz ); break; case 0: fclose( Ft ); return( 0 ); } } return( 0 ); } void Out( TZap z ) { printf( "\n %20s, %d, %d, %d, %d, %d, %d, %1f\n\n\t", z.FIO, z.god_r, z.n_gr, z.ocen_fiz, z.ocen_mat, z.ocen_inf, z.god_r, z.s_b ); fprintf( Ft, "\n %20s, %d, %d, %d, %d, %d, %d, %1f\n\n\t", z.FIO, z.god_r, z.n_gr, z.ocen_fiz, z.ocen_mat, z.ocen_inf, z.god_r, z.s_b ); } //--------------------------------------------------------------------------- 
  • @ixSci if to distract from couple of places with new/delete , the code in fact sishny. - αλεχολυτ
  • @alexolut, yes, but I can not see these two tags in one place. So I try to choose one if it can be done. - ixSci
  • @ixSci you limit access to a question to sshnikam. I wanted to give avp as an example, but he had gold on his pluses, and only silver in the meantime. Amazing. - αλεχολυτ

1 answer 1

In your case, since all data is easily stored in memory, just write down the entire mas_Z . In a temporary file, of course, in case of success - rename temporary to the necessary one, with a backup if necessary.

  • Fz = fopen (File_Zap, "ab"); for (i = 0; i <kol; i ++) {fwrite (mas_Z [i], size, 1, Fz); } fclose (Fz); So the mistake is aaa
  • First, in fwrite() you need to pass the address of the structure element: &mas_Z[i] (what should the compiler tell you, do you read its messages?). Secondly, even if you rewrite the source file immediately, without temporary, then you need to open it in the "write + cut to 0" mode, that is, "wb" , and not to add records to the end ( "ab" ). - PinkTux