My fwrite() is broken???

This is a discussion on My fwrite() is broken??? within the C Programming forums, part of the General Programming Boards category; Ok, first, thank you for taking the time to read this and respond if you do so. This is my ...

  1. #1
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131

    My fwrite() is broken???

    Ok, first, thank you for taking the time to read this and respond if you do so.

    This is my problem, I'm making an address book in C for a class project... This is my problem. It reads the entry from the file, sends it to be edited, gets the edited entry, and the writes it in the spot of the old one. But it doesn't... It writes it at the end of the file... And it's not only here. Anywhere I use fwrite, it is writing to the end of the file...

    This is the code, position is the number of entries away from the beginning of the file the target is. entry is a typedeffed struct...

    binfile is opened in "a+b" mode.

    Code:
    position = PrintList(binfile, position);
    rewind(binfile);
    if(position != -1)
    {
            fseek(binfile, position * sizeof(ENTRY), SEEK_SET);
            fread(&entry, sizeof(ENTRY), 1, binfile);
            entry = EditEntry(entry);
            fseek(binfile, position * sizeof(ENTRY), SEEK_SET);
            fwrite(&entry, sizeof(ENTRY), 1, binfile);
    } /* if */
    Does anyone have any idea what it could be?
    Thanks.

  2. #2
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    This is from "man fopen"

    a+'' Open for reading and writing. The file is created if it does not
    exist. The stream is positioned at the end of the file. Subse-
    quent writes to the file will always end up at the then current
    end of file, irrespective of any intervening fseek(3) or similar.


    You're appending the file. You don't want to do this. If you search the forum, I think I may have remember reading a solution that Prelude eluded to.

  3. #3
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Thanks so much for the help. I'll look through the forum for the solution.
    I never knew that about append, but it makes since... Thanks a lot!
    You wouldn't know of any way to check if a file exists or not, using C or a Unix command?

  4. #4
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    Sure. Try to open it and see if the file pointer is null.

    Code:
    FILE fp*
    
    if ( (fp = fopen("myfiletopen.txt", "r")) != NULL)  {
        /* file exists */
    } else {
        /* file does not exist */
    }
    Last edited by OOPboredom; 04-24-2004 at 10:13 PM.

  5. #5
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Yeah, that's how I was going to do it. But aren't there other reasons it wouldn't open besides it wouldn't exist? Maybe not very likely reasons. I'm sure I probably won't run into them though.
    Thanks again.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    Sure there are other reasons, like rights are insufficient or run out of filedescriptors.
    The later one is a bit rare though you can check for the output of perror() or check the errno variable yourself.

    from man 3 perror
    Code:
           #include <stdio.h>
     
           void perror(const char *s);
    Just call it directly after you tried to open the file.

    Chris
    Last edited by chris78; 04-25-2004 at 08:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using fwrite
    By kenkoh in forum C Programming
    Replies: 10
    Last Post: 02-16-2008, 09:34 AM
  2. WriteFile and fwrite
    By George2 in forum C Programming
    Replies: 4
    Last Post: 08-10-2007, 04:33 AM
  3. fwrite error: bad address
    By icebabe in forum C Programming
    Replies: 6
    Last Post: 05-06-2006, 06:35 AM
  4. Determining fwrite errors
    By cboard_member in forum C Programming
    Replies: 2
    Last Post: 04-26-2006, 05:30 AM
  5. undeclared identifier problem
    By jjj93421 in forum C++ Programming
    Replies: 13
    Last Post: 04-24-2004, 09:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21