Code of 121_5.c ( 121_5.c is a program name)

#include<stdio.h>

typedef struct student
{
    char name[40];
    int age;
}STUDENT;   // size is 44 bytes

int main()
{
    int i;
    char ch;
    STUDENT s[12]={
                     {"ruturaj",0},{"kailsh",1},{"vedant",2},{"sakshi",3},
                     {"aaditya",10},{"tejas",11},{"santosh",12},{"pankaj",13},
                     {"samruddhi",20},{"kristen",21},{"emma",22},{"brad",23}
                                                        
                 },s1;
                 
    FILE *fp;
    fp=fopen("121_para_5.dat","w");   // 121_para_5 is file name
    printf("fp-> %d\n",ftell(fp));

    for(i=0;i<=11;i++)
    {
        fwrite(&s[i],sizeof(STUDENT),1,fp);
        printf("i=%-2d  fp-> %d\n",i,ftell(fp)); // -2d is just for alligen
    }
    fclose(fp);

    fp=fopen("121_para_5.dat","r");

    fseek(fp,220,SEEK_SET);  // at this position null block created in memory
    printf("fseek -> %d\n",ftell(fp)); 

    ch=fgetc(fp);  // retriving character at 220th position
    printf("%d",ch);

    fclose(fp);
}

Output

Output

output in text format

fp-> 0
i=0   fp-> 44
i=1   fp-> 88
i=2   fp-> 132
i=3   fp-> 176
i=4   fp-> 221
i=5   fp-> 265
i=6   fp-> 309
i=7   fp-> 353
i=8   fp-> 397
i=9   fp-> 441
i=10  fp-> 485
i=11  fp-> 529
fseek -> 220
0
--------------------------------
Process exited after 0.6639 seconds with return value 0
Press any key to continue . . .

Now see while writing data in .dat file, I am checking a position where the file pointer is pointing. so file pointer initially pointing to position 0 and position increase by 44 each time, as the size of student data type is 44

but after data enter from 176-219(44 bytes) the 220th position is filled with null, then data writing start normally from 221th position. so my question is why suddenly null character goes at 220th position.

now I increase input data and run loop for more times to check where can I get null again so I got it at 1101,1454,1983,2336,1689,3042......

output when further increase in data

output when further increase in input

output in text mode

fp-> 0
i=0   fp-> 44
i=1   fp-> 88
i=2   fp-> 132
i=3   fp-> 176
i=4   fp-> 221
i=5   fp-> 265
i=6   fp-> 309
i=7   fp-> 353
i=8   fp-> 397
i=9   fp-> 441
i=10  fp-> 485
i=11  fp-> 529
i=12  fp-> 573
i=13  fp-> 617
i=14  fp-> 661
i=15  fp-> 705
i=16  fp-> 749
i=17  fp-> 793
i=18  fp-> 837
i=19  fp-> 881
i=20  fp-> 925
i=21  fp-> 969
i=22  fp-> 1013
i=23  fp-> 1057
i=24  fp-> 1102
i=25  fp-> 1146
i=26  fp-> 1190
i=27  fp-> 1234
i=28  fp-> 1278
i=29  fp-> 1322
i=30  fp-> 1366
i=31  fp-> 1410
i=32  fp-> 1455
i=33  fp-> 1499
i=34  fp-> 1543
i=35  fp-> 1587
i=36  fp-> 1631
i=37  fp-> 1675
i=38  fp-> 1719
i=39  fp-> 1763
i=40  fp-> 1807
i=41  fp-> 1851
i=42  fp-> 1895 
0

There are 0 best solutions below