LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2015, 10:04 AM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
FILE header and stream differentiation


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.
 
Old 08-18-2015, 11:13 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
Old 08-29-2015, 08:37 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
If you are looking for a library:

Code:
apt-get source libmagic-dev
 
Old 08-31-2015, 04:59 PM   #4
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
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?
 
Old 08-31-2015, 05:01 PM   #5
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Guttorm View Post
If you are looking for a library:

Code:
apt-get source libmagic-dev
Sorry, but my knowledge isn't adequate to know what this means, or how it applies to my original question.
 
Old 08-31-2015, 05:55 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by atlantis43 View Post
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.
 
1 members found this post helpful.
Old 09-01-2015, 09:08 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check missing header files included from another header file adisan82 Linux - Software 1 01-28-2011 03:57 AM
Debian Lenny Gnome Desktop Differentiation Capability bruceam Linux - Desktop 2 01-04-2010 01:41 PM
memory file stream mohtashami Programming 2 10-03-2005 01:50 AM
Create header file for existing .c file hk_michael Programming 5 02-25-2005 05:26 PM
[c++] How can I get a file descriptor from a c++ i/o stream? chuanyung Programming 8 03-08-2004 11:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration