ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hope my following dilemma is clear!
I'm used to addressing an overall file (eg: text file)in C with a pointer.
Code:
FILE* fp;
I hope someone can explain (in text form) how, in a file which is supposed to contain a "header" for metadata as well as a "stream" (eg Huffman encoded file or jpeg file), one addresses these separate file sections for reading or writing.
Thanks for any help.
In your program, fp is a pointer to a FILE structure, which is simply the runtime library's data structure for managing access to a file.
Most files contain a fixed "header" portion, usually at the beginning of the file but occasionally at the end, followed by variable data. Simply fseek() to whatever you want.
In your program, fp is a pointer to a FILE structure, which is simply the runtime library's data structure for managing access to a file.
Most files contain a fixed "header" portion, usually at the beginning of the file but occasionally at the end, followed by variable data. Simply fseek() to whatever you want.
With
Code:
int fseek(FILE *stream, long offset, int whence);
I think that I understand that this can be used to go to a chosen position in "stream". My question is more one of how do I initially write a "header" or a "stream" into a FILE denoted by
Code:
FILE* fp
i.e., how do I address these particular portions of the file?
My question is more one of how do I initially write a "header" or a "stream" into a FILE denoted by
Code:
FILE* fp
i.e., how do I address these particular portions of the file?
Have you done IO in C before? FILE* doesn't point to part of a file, or even a file at all. It points to an opaque (meaning the implementation details are inaccessible) structure representing a (metaphorical) tape head which advances through the file as you read or write to it.
To write a header and data to a file, first write the header, then write the data. Maintaining the distinction between header and data is completely up to you. A file is just a series of bytes.
The thing-in-memory called FILE is an internal data structure used by the C runtime-library to manage access by this program to a particular file. It is not a pointer to data within the file.
A common strategy is to (having opened the file for exclusive access) write a dummy header record to the start of the file, then write the stream of data. Finally, fseek() back to the beginning of the file and rewrite the header with the final information.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.