Question about file I/O from a newbie

This is a discussion on Question about file I/O from a newbie within the C Programming forums, part of the General Programming Boards category; Hey all! Got some problems with basic file I/O on binary file. It seems like i can write to the ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    Question about file I/O from a newbie

    Hey all!
    Got some problems with basic file I/O on binary file. It seems like i can write to the file bacause its beeing created. But when i type in information, write it to file, and then reading from file, it seems OK. But if i close the program and try to open the file, all that its printed are just jibberish.

    // My defines
    Code:
    #define TY 20
    #define FY 50
    #define HD 100
    #define TI sizeof(struct TestInfo)
    #define CO sizeof(struct Course)
    My struct:
    Code:
    struct Course
    {
            char kurskod[TY];
            char kursnamn[TY];
            char inst[TY];
            char larare[TY];
            char kurskommentar[FY];
    };
    My write to file:
    Code:
    void write_to_file(struct Course *co)
    {
        FILE *fp;
        fp=fopen("courseinfo.bin", "wb");
        fwrite(&co,CO,1, fp);
        fclose(fp);
    }
    My read from file:
    Code:
    void read_from_file(struct Course *co)
    {
        FILE *inFile;
    
        if(!(inFile = fopen("courseinfo.bin", "r")))
        fread(&co,CO,1,inFile);
    
        fclose(inFile);
    
    }
    The way i call them:
    Code:
    struct Course new_course;
    read_from_file(&new_course);
    write_to_file(&new_course);

    Can anyone see whats from with my functions (or something else for that matter)?

    Regards
    /Henrik!

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    984
    Code:
    if(!(inFile = fopen("courseinfo.bin", "r")))
    fread(&co,CO,1,inFile);
    This doesn't do what you think it does. The following code is equivalent:
    Code:
    inFile = fopen("courseinfo.bin", "r");
    if(inFile == NULL)
    {
      fread(&co,CO,1,inFile);
    }
    You're reading if the file couldn't be opened. It should be the other way around.

    Next, this function (read_from_file()) is being passed a pointer to a struct; you then use the & operator on it, giving you a pointer to a pointer to a struct. Since it's already a pointer, don't use the &. It's also probably a good idea to use sizeof directly with your identifier (variable name), so there's less of a chance for a size mismatch:

    Code:
    fread(co, sizeof *co, 1, inFile);
    The use of & applies to your write_to_file() function, too. As does properly checking fopen()'s return value against NULL.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #define TI sizeof(struct TestInfo)
    #define CO sizeof(struct Course)
    These I don't like the least bit - or in Swedish "Om detta tycker jag inte". I'll continue in English tho', as it may get others to understand what we're talking about.

    Code:
        if(!(inFile = fopen("courseinfo.bin", "r")))
        fread(&co,CO,1,inFile);
    You are reading a binary file without "b" - although a lot of the time it makes no difference, that's bound to bite you sooner or later.

    You are reading into the ADDRESS of the pointer to co - remove the & here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    Quote Originally Posted by matsp View Post
    These I don't like the least bit - or in Swedish "Om detta tycker jag inte". I'll continue in English tho', as it may get others to understand what we're talking about.
    Or rather "detta tycker jag inte om"
    Anyway, I also agree that you should a sizeof the actual variable and not use defines because it's more confusing your way. sizeof *p usually gives you gauranteed correct size of your buffer.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    Talking Thanks!

    Thanks for all the quick replys. I'll shall try to do the things you suggested and learn from that.

    Thanks again!

    /Henrik!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. C++ File I/O question
    By zero_cool in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2005, 10:43 AM
  5. I have a file I/O question as well
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 03:11 PM

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