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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-07-2005, 10:28 AM
|
#1
|
|
Member
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113
Rep:
|
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.
|
|
|
|
12-08-2005, 03:37 AM
|
#2
|
|
LQ Newbie
Registered: Nov 2003
Posts: 19
Rep:
|
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.
|
|
|
|
12-08-2005, 06:08 AM
|
#3
|
|
Member
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113
Original Poster
Rep:
|
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.
|
|
|
|
12-08-2005, 07:08 AM
|
#4
|
|
LQ Newbie
Registered: Nov 2003
Posts: 19
Rep:
|
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.
|
|
|
|
12-08-2005, 12:48 PM
|
#5
|
|
Member
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113
Original Poster
Rep:
|
Thanks doraiashok.
A little more reading and experimenting revealed that any type of structured data can also be dealt with by fread()/fwrite().
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:23 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|