Find the answer to your Linux question:
Results 1 to 7 of 7
hi I am trying to write an entire structure into afile. But when i uuse Code: fwrite(&ve ,sizeof(ve),fp); error : fwrite has one less argument happens where ve is structure ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2003
    Location
    India
    Posts
    20

    help with c code


    hi
    I am trying to write an entire structure into afile. But when i uuse
    Code:
    fwrite(&ve ,sizeof(ve),fp);
    error : fwrite has one less argument happens
    where ve is structure variable and fp is file descriptor.
    soI put one more argument

    Code:
    fwrite(&ve,sizeof(ve),1,fp)
    and segmentation error comes............please help..

  2. #2
    Just Joined!
    Join Date
    Jun 2003
    Location
    Helsinki, Finland
    Posts
    78
    Maybe try write instead?

    Code:
    $ man 2 write
    ...
    ssize_t write(int fd, const void *buf, size_t count);
    Also, I don't know about your "sizeof(ve)". I prefer the notation "sizeof( struct myStruct )"

    More understandable IMHO.

    As for the segfault, did you open the file first?

  3. #3
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    sizeof(ve) is perfectly good C. I really do agree with you, caffeine, but on the other hand sizeof(ve) holds up even if you change the type of ve.

  4. #4
    Just Joined!
    Join Date
    Jun 2003
    Location
    Helsinki, Finland
    Posts
    78
    Cool. I always thought it was a weird side effect that just happened to work, but I guess not. How does sizeof work if you change the type of a variable? Wouldn't sizeof simple return the sizeof the type the variable is declared as? What happens if you cast a void * to a some structure and call sizeof? I guess I'll have to test!

    Cheers.

  5. #5
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Now, I haven't examined sizeof that closely, but I guess that if you don't pass it a type name, it evaluates the expression inside the parentheses and returns the size of the final type of it. That means, of course, that sizeof((void *)ve) would return 4 on any IA32 architecture. Always remember, though, that sizeof isn't a function, but a language keyword.

  6. #6
    Just Joined!
    Join Date
    Jun 2003
    Location
    Helsinki, Finland
    Posts
    78
    Yeah. I think the preprocessor actually substitutes calls to it with the actual size before the code is compiled.

  7. #7
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Oh no. The preprocessor does nothing with it. Remember, it couldn't possible evaluate variables.

Posting Permissions

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