c - When the file pointer reads the file over the border? -


i first read file in binary, when read end of file, file closed, visual stdio 2013 error pointer on border.

file *fp; fp = fopen("stu_manage.txt", "ab+"); fseek(fp,0l,seek_end); long last = ftell(fp) / length; long = 0l; rewind(fp); struct student *node=(struct student*)malloc(sizeof(struct student)); (; < last; i++) {     if (fread(&node[i], length, 1, fp) != 1)     {         printf("read conpletely");         break;     } } if (fp!=null)    fclose(fp); 

when put here pointer change global variables, error solved, array in degree equivalent pointer here not run feel unable understand.

in code, you're allocating memory 1 variable of type struct student in node, you're accessing out-of-bound memory using incremental index i in

if (fread(&node[i], length, 1, fp) != 1) 

which in turn invokes undefined behaviour.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -