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 write a simple bit of test code to save a variable to a file. So each time the variable is written it is updated in the file. This simple code just increments a global variable counter, and calls the function. In the function I read the file getting the value of counter increament it and write it back to the file. I thought this would be fairly simple, however when I printf it always shows a value of counter =0
my function is
[source]
void savecounter(void)
{
FILE *fp;
counter[0]=0;
if((fp=fopen("/usr/local/counter", "w+b"))==NULL) /*count to events*/
{
printf("cannot open file\n");
return;
}
fread(counter, sizeof(counter[0]), sizeof(counter)/sizeof(counter[0]), fp); /*read counter from file*/
printf("counter %d\n",counter[0]); /*print the value*/
counter[0]++; /* inc &counter*/
rewind(fp); /*go to start of file*/
fwrite(counter, sizeof(counter[0]), sizeof(counter)/sizeof(counter[0]), fp); /*and write*/
fclose(fp);
}
[/source]
I call it from 5 times so would think I'd get counter = 0, counter =1 counter = 2.....
however all I get is counter = 0 everytime, I've tried taking out rewind, I've also called counter as & and used size of int, in my code.
so
FILE *fp;
counter=0;
if((fp=fopen("/usr/local/counter", "wb+"))==NULL) /*count to events*/
{
printf("cannot open file\n");
return;
}
fread(&counter, sizeof(int), 1, fp); /*read counter from file*/
printf("counter %d\n",counter); /*print the value*/
counter++; /* inc &counter*/
rewind(fp); /*go to start of file*/
fwrite(&counter, sizeof(int), 1, fp); /*and write*/
fclose(fp);
Can anyone help me out or point me in the direction. I should point out debugging in GDB show my file goes to 1 when I write, however when I read on the second function call he value is set to 0. Thanks in advance for any help.
man fopen and look at your savecounter open mode. Can you see what you are doing? even tho I can not see you using the function.
Counter seems to be a global pointer or an array, yet you NULL it before "count to events" and pass the address of the address?
Your problem lies not in fread nor fwrite, but in fopen. When you initially open with with "wb+" the existing file is truncated. Thus the fread() will always fail…
Thanks it does seem to be my open, which I can't get to work with wb+ either, I have got it workig by opening the file with read only, then loading the data. Then closing the file. changeing the data, then opening the file for write only. This works a treat. but is slightly long winded, so I use
[source]
counter=0;
if((fp=fopen("/usr/local/counter", "rb"))==NULL)
{
printf("cannot open file\n");
return;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.