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 04-18-2007, 08:57 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
help understanding fread/write in C


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.

Last edited by knobby67; 04-18-2007 at 09:08 AM.
 
Old 04-18-2007, 10:54 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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?

Last edited by dmail; 04-18-2007 at 10:56 AM.
 
Old 04-18-2007, 10:58 AM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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…
 
Old 04-18-2007, 11:10 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Your problem lies not in fread nor fwrite,
Maybe I am missing information you have then, personally I think there is problems with it as well as the mode.

Quote:
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

counter=0;
...
printf("counter %d\n",counter[0]); /*print the value*/
...
fread(&counter, sizeof(int), 1, fp); /*read counter from file*/
printf("counter %d\n",counter); /*print the value*/
counter++; /* inc &counter*/
...
 
Old 04-19-2007, 03:41 AM   #5
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
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;
}

fread(&counter, sizeof(int), 1, fp);
printf("counter %d\n",counter);
fclose(fp);

if((fp=fopen("/usr/local/counter", "wb"))==NULL)
{
printf("cannot open file\n");
return;
}


counter++;

fwrite(&counter, sizeof(int), 1, fp);
fclose(fp);
[/source]
 
Old 04-19-2007, 05:05 AM   #6
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
open file in read write mode
and use a rewind between reading and writing
 
  


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
problem with fread function nesta Programming 4 04-02-2007 01:10 PM
using fread with fscanf andystanfordjason Programming 4 12-20-2006 09:49 AM
C programming fread and fwrite using structures exvor Programming 4 09-26-2006 08:56 AM
UT: "fread failed: BufferCount = -248172 Error=1" occurs after gfx card change SBing Linux - Games 2 07-04-2004 04:40 PM
fread magic: Input/Output error LoungeLizard Linux - Hardware 1 02-24-2004 06:21 PM

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

All times are GMT -5. The time now is 12:41 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