0

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 
Abhishek Mane
  • 619
  • 7
  • 20
  • This looks very weird... What does [`fwrite`](https://en.cppreference.com/w/c/io/fwrite) return? Please check that. – Some programmer dude Feb 26 '21 at 12:36
  • By the way, note that [`ftell`](https://en.cppreference.com/w/c/io/ftell) returns a `long` value, so the correct format to print its result would be `%ld` (mismatching format specifier and argument type leads to undefined behavior). – Some programmer dude Feb 26 '21 at 12:38
  • @Someprogrammerdude I corrected my mistake by replacing %ld but still same problem. – Abhishek Mane Feb 26 '21 at 12:51
  • `// size is 44 bytes`? How do you know that? – Andrew Henle Feb 26 '21 at 13:36
  • Please do not add text output as images. Instead just copy&paste it direcly as formatted text into the question. – Gerhardh Feb 26 '21 at 14:20
  • @AndrewHenle student data type defined with char name[40] and int age, so 40 bytes for char type arrray and 4 bytes for int so 40+4=44 – Abhishek Mane Feb 26 '21 at 15:17
  • @Gerhardh i think output image can grasp easily rather than text – Abhishek Mane Feb 26 '21 at 15:19
  • You should be aware that text is easier to handle for everyone who might want to help you. Embedded pictures of your text are way too tiny to read and by this you force readers to open in extra window. That will make some people simply ignoring your question. – Gerhardh Feb 26 '21 at 15:23
  • @AbhishekMane Summing up the size of the elements of a `struct` in order to get its size is wrong. See [**Why isn't sizeof for a struct equal to the sum of sizeof of each member?**](https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member) – Andrew Henle Feb 26 '21 at 15:52
  • @Gerhardh I got your point, I corrected it . – Abhishek Mane Feb 26 '21 at 15:52
  • @AndrewHenle I get your point but in this case paddling is not happen as we have 40 characters in array .if char array size is like 39,38 or 37 then there will be case of paddling – Abhishek Mane Feb 26 '21 at 15:56
  • 2
    If you are using Windows, you should open files in binary mode if you store raw data in it. I assume in your file you will find any `0x0D` preceded by a `0x0A` if you open in hex editor. – Gerhardh Feb 26 '21 at 16:13
  • You might inspect a hex dump of your file to see where exactly you got extra bytes. – Gerhardh Feb 26 '21 at 17:01

0 Answers0