I open a file in text mode(for example mode "r+"),and then I use
fwrite
function and write a
struct
into a file.Now when I look inside the file with Notepad(for example),I can't read the entries.So they are not text files ,maybe binary files?But I didn't open the file in binary mode?
My next question is,if I open a file in text mode ,if I write using
fwrite
and structs,try to read it's entries using
fread
, and if there is the number "26" inside one of the structs,then it continues reading up to 26,and without printing 26 and next numbers,it stops reading from file..
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <stdio.h>
#include <stdlib.h>
struct structure
{
int num;
}structure1;
int main()
{
FILE* fp;
if ((fp = fopen("file1.ntt","w+")) ==0)
{
exit(1);
}
for (int i=1;i<45;i++)
{
structure1.num=i;
fwrite(&structure1,sizeof(struct structure),1,fp);
}
rewind(fp);
while(feof(fp)==0)
{
fread(&structure1,sizeof(struct structure),1,fp);
printf("%d",structure1.num);
}
}
|
This program reads until num value becomes 26,then it stops reading.
I have read that character "z" (and it's ascii code "26") is used as end of file indicator.So this looks like the reason of the problem,but I haven't been able to find a solution other than simply opening the file in binary format(for example w+b) or using functions other than
fwrite
.