Thread: Help With fseek();

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    19

    Help With fseek();

    Hello, I'm having problems learning about rand access files...

    Most of my code works, all until you get to the fseek. The output is all garbaled, I'm not sure why.

    I'll post the entire code, so that if it's something other than the fseek that is affecting it, you could point that out...

    Thanks
    Code:
     #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define PASS1 "daniel"
    #define PASS2 "pword24"
    #define INVARY 10
    #define TRANS 1000
    
    struct inventory {
        char desc[25];
        int invCode;
        int invAmount;
        float invPrice;
    };
    
    struct transaction {
        char desc[25];
        int invCode;
        int invAmount;
        float invPrice;
    };
    
    void printInfo(struct inventory inv_db_new);
    
    FILE * disk_file;
    
    main()
    {
        //Variables
        int counter;
        //Create arrays of the stuctures
        struct inventory inv_db[INVARY] = {{"Widget", 1001, 10, 2.95},
                                           {"Widget2", 1002, 10, 5.20},
                                           {"Widget3", 1003, 10, 10.00},
                                           {"Widget4", 1004, 10, 4.56},
                                           {"Widget5", 1005, 10, 9.99},
                                           {"Widget6", 1006, 10, 4.44},
                                           {"Widget7", 1007, 10, 70.02},
                                           {"Widget8", 1008, 10, 45.66},
                                           {"Widget9", 1009, 10, 34.55},
                                           {"Widget10", 1010, 10, 30.23}};
        
        struct inventory inv_db_new[INVARY];
        struct inventory aVar;
        struct transaction trans_db[TRANS];
        
        disk_file = fopen("c:\\may.inv", "w+");
            if (!disk_file) {
            printf("Error, with disk\n");
            }
        printf("Success, with disk\n");
        fwrite(inv_db, sizeof(inv_db), 1, disk_file);
        printf("Success in writing to disk\n");
        fclose(disk_file);
        
        disk_file = fopen("c:\\may.inv", "r+");
            if (!disk_file) {
            printf("Error, with disk\n");
            }
        fread(inv_db_new, sizeof(inv_db_new), 1, disk_file);
        for (counter = 0; counter < INVARY; counter++) {
        printInfo(inv_db_new[counter]);
        }
        fclose(disk_file); 
        
        disk_file = fopen("c:\\may.inv", "r+");
            if (!disk_file) {
            printf("Error, with disk\n");
            }
        
        fseek(disk_file, (4L * sizeof(aVar)), SEEK_SET);
        fread(&aVar, sizeof(aVar), 1, disk_file);
        printInfo(aVar); 
        fclose(disk_file);    
    }
    
    void printInfo(struct inventory str_nam)
    {
        printf("\n%d ", str_nam.invCode);
        printf("%s ", str_nam.desc);
        printf("%d ", str_nam.invAmount);
        printf("$%.2f\n", str_nam.invPrice);
        return;
    }

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    the trouble you're experiencing is being caused by the way you are opening the files. You are using the r+ and w+ ways of opening files (and these methods are used for "updating" files...or something like that)

    The type of read/write style you're looking for in this case is "rb" and "wb" for read/write binary.

    Code:
    fopen("c:\\may.inv", "r+");
    change that to:

    Code:
    fopen("c:\\may.inv", "rb");
    and do the same with the w+ one, change it to a wb

    hope that helps....

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    19
    Cool. Thanks for the help!

    It's kindof odd though, that source isn't much different than the source from the book I'm learning from. I have char strings ints and floats in my struct, but they only have strings and ints... Maybe you need to use binary if your structs have floats?

    http://www.cppreference.com/stdio_details.html#fopen

    Has a list of all the options.
    Last edited by Explicit; 05-26-2004 at 08:34 PM.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    don't forget to exit your program when it doesn't open correctly. You warn them but don't quit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fseek failing - Error: Invalid argument
    By avi2886 in forum C Programming
    Replies: 14
    Last Post: 05-14-2009, 08:49 PM
  2. question about fseek
    By gp364481 in forum C Programming
    Replies: 6
    Last Post: 11-07-2008, 01:45 PM
  3. fseek question
    By blondie.365 in forum C Programming
    Replies: 19
    Last Post: 10-20-2008, 04:30 PM
  4. fseek() changing an unrelated variable?
    By aaronvegh in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 02:30 PM
  5. popen and fseek
    By mach5 in forum C Programming
    Replies: 4
    Last Post: 11-29-2003, 02:03 AM