C fread/fwrite problem

This is a discussion on C fread/fwrite problem within the C Programming forums, part of the General Programming Boards category; I have a program that makes a database of file signatures, using stat. I have a record of useful information ...

  1. #1
    Registered User methodmza's Avatar
    Join Date
    May 2002
    Posts
    8

    C fread/fwrite problem

    I have a program that makes a database of file signatures, using stat. I have a record of useful information and dump this, including the time last modified as an array of 50 characters, which i store the ctime of the time last modified into, which is about 25 characters. my problem is when i dump the signature record into my database file using fwrite, itll dump an extra 25bytes of crap after the time string, so that wen i compare this string to another ctime generated string that is exactly the same, the memory after it is different, as it is random bytes from memory. i have tried using strncpy strcpy memcpy memccpy etc. and cannot get it working. i dont expect anyone to write my code :P jus a tip as to wat function could help would be nice. im using C in linux if it matters. thanks a lot.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    So you're writing too much bytes to the file. Can you post the code which does that?

  3. #3
    Registered User methodmza's Avatar
    Join Date
    May 2002
    Posts
    8
    Code:
    if (fp = fopen(database,"a"))
                    {
                            fwrite(&sig,sizeof(sig),1,fp);
                    }
                    fclose(fp);
    }
    heres the typedef for sig:
    Code:
    typedef struct
    {
            char name[256];                 /*File or directory name*/
            u_short mode;                   /*protection and file type*/
            short user_id;                  /*User ID of owner*/
            short group_id;                 /*Group ID of owner*/
            off_t size;                     /*file size in bytes*/
            char time_last_mod[50];         /*modify time as a string*/
    }file_sig;
    as u can probably tell this is an assignment :P thats why i dont want u to gimme any code, simply a suggestion or clue. since i posted ive also tried making a function to pad the end of the 50 length char array with '\0' but that didnt work either for some reason.

    to read the record im doing jus the opposite with fread, reading the sizeof(sig) bytes from the database.

  4. #4
    Registered User methodmza's Avatar
    Join Date
    May 2002
    Posts
    8
    also...i kno it would be easy to limit time_last_mod to 25 bytes, but I cant, the typedef is a mandatory typedef we have to use. i think theyre trying to create this problem on purpose :P

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    So if I am correct then only be the first 25 bytes of time_last_mod should be written to the file and you're not allowed to change the size of time_last_mod?

    In that case I would not use fwrite to write the whole struct at once, but write each struct member one by one to the file. And for time_last_mod you could use a for-loop to write the first 25 bytes to the file.

  6. #6
    Registered User methodmza's Avatar
    Join Date
    May 2002
    Posts
    8
    now why the **** didnt i think of that :P thanks a lot, i cant be bothered testing atm...but that certainly sounds right to me ) ill post a msg saying how i go, i should do it in the next 2 days cos thats how long ive got till its due.

  7. #7
    Registered User methodmza's Avatar
    Join Date
    May 2002
    Posts
    8
    well that didnt quite work...i dont kno y, im sure if i stuck at it i was doin sumfin rong. but i fixed the prob anyway. i simply wrote and read the full record and used strncmp :P comparing only the first 24 bytes, 25 but ignoring the \n from ctime. so simple in the end thanx anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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