[RESOLVED] fwrite not working
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: [RESOLVED] fwrite not working

  1. #1
    Join Date
    Jun 1999
    Posts
    490

    [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.

  2. #2
    Join Date
    Apr 2008
    Posts
    726

    Re: fwrite not working

    The file is being written to, so it must exist. Where did you look for the file?

  3. #3
    Join Date
    Jun 1999
    Posts
    490

    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.

  4. #4
    Join Date
    Apr 2008
    Posts
    726

    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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,434

    Re: fwrite not working

    Quote Originally Posted by Zulfi Khan2 View Post
    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

  6. #6
    Join Date
    Jun 1999
    Posts
    490

    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.

  7. #7
    Join Date
    Apr 2008
    Posts
    726

    Re: fwrite not working

    doesnt structure size depend on padding?

  8. #8
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,051

    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.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  9. #9
    Join Date
    Apr 1999
    Posts
    27,434

    Re: fwrite not working

    Quote Originally Posted by Zulfi Khan2 View Post
    Hi,
    For the above structure, it should be 36.
    No.

    The size of that struct is this:
    Code:
    sizeof( lib )
    Regards,

    Paul McKenzie

  10. #10
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,051

    Re: fwrite not working

    Quote Originally Posted by Paul McKenzie View Post
    No.

    The size of that struct is this:
    Code:
    sizeof( lib )
    Yes. For that reason the sizeof operator has been invented.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  11. #11
    Join Date
    Jun 1999
    Posts
    490

    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.

  12. #12
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,621

    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.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,434

    Re: fwrite not working

    Quote Originally Posted by Zulfi Khan2 View Post
    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.

  14. #14
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,051

    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.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  15. #15
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,051

    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
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center