Click here to Skip to main content
15,891,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am working on a database flat file project using c language .i have created a structure with few members . i am using fwrite() to write them in binary file and fread() to fetch the data .My two major question

1st can we write structure in text file ? i have seen no good example .is it practically wrong to write it in text format ?

2nd how these fread() & fwrite works().they operate on a block of data how they get the address of next block .i mean we do have the pointer but file doesnt have any address so how the pointer go to next block ?

What I have tried:

i have tried it works but why and how ?
Posted
Updated 12-Jul-16 11:02am

That's callsed Serialization, see Serialization - Wikipedia, the free encyclopedia[^].

Quote:
is it practically wrong to write it in text format ?
No, it is not wrong. In fact, some serialization mechanisms use text files. It is a bit inefficient, though.

Have a look at this page: C - serialization techniques - Stack Overflow[^].
 
Share this answer
 
Quote:
1st can we write structure in text file ?

Yes you can.
Quote:
i have seen no good example .

This your design choice. You decide how the structure translate to file and how you get it back to structure.
XML files are used for this all the time. Structures are translates to XML and vice-versa.
Quote:
is it practically wrong to write it in text format ?

You design choice. See XML files.
Quote:
2nd how these fread() & fwrite works().they operate on a block of data how they get the address of next block .i mean we do have the pointer but file doesnt have any address so how the pointer go to next block ?

Looks like you need to go back to C/C++ basics
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
 
Share this answer
 
1st can we write structure in text file ? i have seen no good example .is it practically wrong to write it in text format ?


If, by structure, you mean binary data (eg. not ASCII), no you should not use text mode. Open the file as binary if you want to operate on bytes without the C runtime applying some rules (eg. "cooked" data) that effect carriage return, line feed, and - for some platforms - end of file marker.

2nd how these fread() & fwrite works().they operate on a block of data how they get the address of next block .i mean we do have the pointer but file doesnt have any address so how the pointer go to next block ?


The file system takes care of this. There's an implicit address or file offset where the data is stored. Each call to fwrite causes the offset to move so, by default, you are always appending data to the file. You can also change this offset by using fseek and examine the offset by using ftell. You do not need to worry about how disk sectors, blocks, or clusters are organized.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900