LinuxQuestions.org
Review your favorite Linux distribution.
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 06-22-2009, 08:12 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
writting multiple variables to a single file


Hi all,
I'm trying to solve a problem of writting multiple variables to a single file. Normally I'd use fwrite to write a single variable, however I'd like to write multiple variables to a single file, for example int x,y,z, double a,b, struct m,n. Is there a wayto do this?

The reason I'm doing this is I'm moving from using NVRAM (mmapped) on an ARM board to uing a board with no NVRAM and a harddrive so I need to store about 30 variables that are normally written to NVRAM. I'm an embeedded programmer so my knowledge of file functions is limited.

Thanks.
 
Old 06-22-2009, 10:00 AM   #2
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
So 'fprintf' is out of the question, or did I misunderstood your question ? (I have no knowledge about embedded programming in detail, so my answer might be quite naive).
 
Old 06-22-2009, 12:37 PM   #3
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Hi Thanks,
What I was looking for was writing one variable at a time rather than a stream, but I supose I could do a new line for each variable then when I read new line to each varaible line number and read the value from there?
 
Old 06-22-2009, 12:54 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
So your question is about how to get back the value of variables you wrote in? (My first reaction to your question was "call fwrite() again...")

You have remember which offset you wrote to, then you can use fseek to move the stream there and fread to get the variable. Or read in the same order you write.

If your data is fixed length and not too big, I would suggest packing everything in one struct. Harddrives usually can't read/write less than 512 bytes at a time (although maybe your embedded system is different, I think flash drives can read in smaller chunks).
 
Old 06-22-2009, 08:46 PM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
As a matter of fact this question is simple so I am afraid I don't get it correctly.

Would it be possible to use
Code:
fprintf(fp, "%d, %d, %d, %f, %f, %f %d\n", x, y, z, a, b, struct.floatfield, struct.intfield);
where fp is a pointer to a file you opened with fopen.
You can also write multiple fprintf values one by one, up to you whether or not you terminate with a newline.

To get those values back you could use exactly the inverse of fprintf, fscanf. Since you wrote the values yourself you know exactly the amount and the format, hence it is a fairly safe operation.

jlinkels
 
Old 06-23-2009, 01:10 AM   #6
rapa
LQ Newbie
 
Registered: Jun 2009
Posts: 7

Rep: Reputation: 1
Use 'sprintf' to any string and then 'fprintf'.
Or directly,instead 'fprintf', use 'write'
 
  


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
Copying single file to multiple locations Ashiro Linux - Server 4 12-05-2008 05:50 AM
Is multiple owners of a single file possible? can user1 own a file that root also own dave247 Debian 3 10-09-2008 04:53 AM
Copying a single file to multiple directories quest49 Linux - Software 6 12-04-2006 08:07 AM
Adding Multiple Files In A Single Gz File onacorpuscle Linux - Newbie 3 08-31-2006 03:18 AM
Copying a single file to multiple directories tgolly Linux - Newbie 3 04-26-2004 03:47 PM

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

All times are GMT -5. The time now is 03:27 PM.

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