-
April 16th, 2011, 12:51 PM
#1
[RESOLVED] fwrite not working
Hi,
I am trying to create a file using fwrite. I am using VS2008 & Vista,
Code:
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
struct lib{
char bname[30];
int id;
float price;
};
int _tmain(int argc, _TCHAR* argv[])
{
FILE* fp;
struct lib libVar;
int k;
fp=fopen("data.tmp", "wb+");
if(fp==NULL)
printf("Error");
printf("Enter book name\n");
gets(libVar.bname);
printf("Enter id\n");
scanf("%d", &libVar.id);
printf("Enter price\n");
scanf("%f",&libVar.price);
k=fwrite(&libVar,sizeof(libVar), 1, fp);
printf("k=%d\n", k);
fclose(fp);
return 0;
}
I CANT SEE THE FILE. k=1 printed. Can somebody plz help me to find out the problem why this file is not being created???
Zulfi.
-
April 16th, 2011, 01:13 PM
#2
Re: fwrite not working
The file is being written to, so it must exist. Where did you look for the file?
-
April 16th, 2011, 01:41 PM
#3
Re: fwrite not working
I made a search. I checked in the project folder. I ran the same program with Turbo C also. Its creating file in TC folder but its 0 bytes.
Kindly guide me.
Zulfi.
-
April 16th, 2011, 03:14 PM
#4
Re: fwrite not working
the file will be created in the current working directory, so it depends where the program is run from...
I just ran your code. it made a file with non-zero size. You must be looking at the wrong file.
-
April 16th, 2011, 05:19 PM
#5
Re: fwrite not working
Originally Posted by Zulfi Khan2
I made a search. I checked in the project folder. I ran the same program with Turbo C also. Its creating file in TC folder but its 0 bytes.
Why not remove all doubts, and use full path names?
Code:
fp=fopen("c:/some_directory/data.tmp", "wb+");
Now you know where the file will be created.
Regards,
Paul McKenzie
-
April 17th, 2011, 12:46 PM
#6
Re: fwrite not working
Hi,
Thanks for your help. I was using wrong slashes and the code for TC also misses the fwrite command. fwrite is returning 1 maybe because its 1 record.
fread() and fwrite() return the number of items successfully read or
written (i.e., not the number of characters).
If I check the properties file size is 40 bytes.
Code:
struct lib{
char bname[30];
int id;
float price;
};
For the above structure, it should be 36.
Zulfi.
-
April 17th, 2011, 02:29 PM
#7
Re: fwrite not working
doesnt structure size depend on padding?
-
April 17th, 2011, 02:38 PM
#8
Re: fwrite not working
The size of a structure is not obviously equal to the sum of its member sizes because they are aligned on a given boundary.
See Structure Alignment.
-
April 17th, 2011, 03:53 PM
#9
Re: fwrite not working
Originally Posted by Zulfi Khan2
Hi,
For the above structure, it should be 36.
No.
The size of that struct is this:
Regards,
Paul McKenzie
-
April 18th, 2011, 08:11 AM
#10
Re: fwrite not working
Originally Posted by Paul McKenzie
No.
The size of that struct is this:
Yes. For that reason the sizeof operator has been invented.
-
April 18th, 2011, 12:48 PM
#11
Re: fwrite not working
Hi,
Thanks for your attention. I dont know how this sizeof works in VC but in turboc its giving me 36.
Zulfi.
-
April 18th, 2011, 01:27 PM
#12
Re: fwrite not working
I would guess that sizeof(int) = 2 for turboc.
Most compilers will align on 4 or 8 byte boundaries, unless
the programmer specifies otherwise.
-
April 18th, 2011, 01:32 PM
#13
Re: fwrite not working
Originally Posted by Zulfi Khan2
Hi,
Thanks for your attention. I dont know how this sizeof works in VC but in turboc its giving me 36.
The sizeof() works as its supposed to work.
The number of bytes that structure takes up is sizeof(that struct). It isn't 36, 38, 40, or whatever other "hard-coded" number you can think up. If you get an interview question, and the interviewer shows you a struct or class and asks you "how big is this struct s, in bytes?", there is only one right answer -- sizeof( s ). If you sit there and start counting the members and adding up a number, a competent interviewer will clearly see you're either a beginner or not a very good C or C++ programmer.
The compiler is free to add whatever space in-between and/or after the members. Usually, the compiler will attempt to align the members in memory so that it is advantageous for the underlying OS to access that member. Therefore the total number of bytes that the struct takes up is whatever the compiler says it is, because it is the compiler that is responsible for laying out the members in memory in whichever way it sees fit.
Last, as Philip pointed out, ints, floats, doubles, etc. can be different sizes depending on compiler and environment. Unlike Java, C++ has no set size for data types (except for char). There are rules in C++ stating which types must be at least as large as another type, but no concrete number as to how large an individual data type should be (again, except for char, which must be 1 byte).
Regards,
Paul McKenzie
Last edited by Paul McKenzie; April 18th, 2011 at 01:37 PM.
-
April 18th, 2011, 01:47 PM
#14
Re: fwrite not working
Once, I was asked at an interview "Which is the size of char, short, int, and long".
My answer was "I don't care because I have the sizeof operator".
"Hmmm... depends..." said the interviewer and I didn't get the job.
-
April 18th, 2011, 02:08 PM
#15
Re: fwrite not working
Resuming by short what Paul already stated:
The only one C/C++ type for which the sizeof is guaranteed known is (signed, unsigned) char.
The rest are implementation-defined, i.e. depend on each particular compiler.
Last edited by ovidiucucu; April 18th, 2011 at 02:11 PM.
Reason: typo
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|