In case 3 on input, xedit gives an error

//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop //--------------------------------------------------------------------------- #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; int xedit = 0; 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", &xedit ); printf( "%d", &xedit ); /* switch() { case 1: printf("Vvedite FIO"); //printf("\n\t");gets(afio); break; case 0: return(0); }*/ } 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 ); } //--------------------------------------------------------------------------- 

    1 answer 1

    The error "Access violation ..." may indicate that somewhere before its occurrence you are "stealing" memory. The following things are immediately apparent in case 3 code:

    1. Double open File_Zap file for reading
    2. Calling the insecure gets() function
    3. No fread() return checks

    The general way to look for similar errors: first load the program in the debugger, go through the steps, and carefully look at the state of the variables with your own eyes, what is written where, if the buffers do not overflow, damage the previous set values, etc. If for some reason this does not help, then you can use the utilities to search for errors in working with memory. Apparently, we are talking about Windows, so Valgrind for Windows and a large list of others are here .

    • 1 and 2 points did not give any result - aaa
    • That is, you 1) did not find anything suspicious under the debugger and 2) none of the ten utilities showed any errors? - PinkTux
    • Is it really 1 and 2? I closed File_Zap and changed the function to scanf - aaa
    • Yes, these are the first and second points in the search path for such errors. - PinkTux
    • Your answer does not solve the problem. - aaa