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.
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.
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).
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?
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).
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.