LinuxQuestions.org
Register a domain and help support LQ
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 12-07-2005, 10:28 AM   #1
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Rep: Reputation: 15
fwrite() doe not move pointer, why?


I am using C++BuilderX and am facing some problems while working with files.
My understanding was that fwrite() moves the pointer when writing something in a file.

Code:
 
char person[30];
char read_person[30];
FILE *fp;
fp = fopen("myfile.dat","wb+");

...(I read something in string person from stdin)

 fwrite(&person,sizeof(person),1,fp);
  fread(&read_person,sizeof(read_person),1,fp);
fclose(fp);
Don't I need to go back to the beginning of string, i.e. with fseek(),in file befor I can read it with fread()?

Any idea or hint is why fread() is reading the string I entered in file withoug use of fseek()?

Thanks in advance.

regards,

Last edited by realos; 12-07-2005 at 11:42 AM.
 
Old 12-08-2005, 03:37 AM   #2
doraiashok
LQ Newbie
 
Registered: Nov 2003
Posts: 19

Rep: Reputation: 0
you are passing &person and &read_person to fwrite and fread respectively which are (char **). you should have passed person and read_person.

I claim the behaivour to be undefined in this case because all you are reading is some garbage characters.
 
Old 12-08-2005, 06:08 AM   #3
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Original Poster
Rep: Reputation: 15
Well, I was trying to read a whole struct with fread() and fwrite(). It does not seem to work.

Code:
typedef struct  	{
                char 	vorname[30];
                char 	name[30];
                char 	strasse[50];
                char 	plz[10];
                char 	ort[30];
}ADRESSE_t;


ADRESSE_t person;

fwrite(&person,sizeof(person),1,fp);
fread(&read_person,sizeof(read_person),1,fp);
In order to cut short the code for forum questions, I did not mention that person is a struct.
Actually, sizeof() works fine with struct. Maybe fread() and fwrite() can't?? I also tried without the & sign but to no avail.

Definition of fread() and fwrite() suggests buffer could be anything:
Code:
  int fread( void *buffer, size_t size, size_t num, FILE *stream );
  int fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
What kind of buffer is possible in fread()/fwrite()? Only standard variables like int, char, char[] or also made-up structs??

Can anybody clarify on this?

Thanks in anticipation.

Last edited by realos; 12-08-2005 at 06:12 AM.
 
Old 12-08-2005, 07:08 AM   #4
doraiashok
LQ Newbie
 
Registered: Nov 2003
Posts: 19

Rep: Reputation: 0
yes, you can read/write structures into file using fread/fwrite.

In this case even, you would need to use fseek to read back the data written.
 
Old 12-08-2005, 12:48 PM   #5
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Original Poster
Rep: Reputation: 15
Thanks doraiashok.
A little more reading and experimenting revealed that any type of structured data can also be dealt with by fread()/fwrite().
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
move mouse pointer from shell script malo_umoran Slackware 1 11-06-2004 08:31 AM
how to move pointer w/o using mouse in X gongli Linux - Software 1 10-22-2003 07:42 PM


All times are GMT -5. The time now is 10:04 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration