Thread: Random File Issues

  1. #1
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Exclamation Random File Issues

    Here is a struct I use to write to a random file:

    ------------------------------------------------------------------------

    typedef struct {
    int eqNo;
    char eqName[20];
    float price;
    int stock;
    int reorder;
    } equipment;

    ------------------------------------------------------------------------

    Here is the variable definition:

    ------------------------------------------------------------------------
    equipment Equip = {0,"",0.0,0,0};
    ------------------------------------------------------------------------

    I open the file like this:

    ------------------------------------------------------------------------
    fPtr = fopen(filename, "r+");
    ------------------------------------------------------------------------

    NOTE: filename is a string variable with the actual filename

    This prints the data stored in this struct for individual records from the random file just fine:

    ------------------------------------------------------------------------
    printf("1 - Item # : [%d]", Equip.eqNo);
    printf("2 - Name : [%s]", Equip.eqName);
    printf("3 - Price : [%.2f]", Equip.price);
    printf("4 - In Stock : [%d]", Equip.stock);
    printf("5 - Reorder : [%d]", Equip.reorder);

    ------------------------------------------------------------------------

    But this, when reading from the random file, prints the first few records, and then gibberish...

    ------------------------------------------------------------------------
    while(!feof(fPtr))
    {
    fread(&Equip, sizeof(equipment), 1, fPtr);
    if (Equip.eqNo != 0)
    {
    printf("%-7d%-20s%-10.2f%-9d%-8d",Equip.eqNo,
    Equip.eqName, Equip.price, Equip.stock, Equip.reorder);
    }
    }

    ------------------------------------------------------------------------

    The above while loop code works for all of my other structures and random files EXCEPT the structure mentioned above.

    So, what am I doing wrong with that loop? Is there something wrong with this loop or is there something wrong with the structure? If you need any more code to help your reasoning, just ask. Thanks in advance.
    Excuse me, while I water my money tree.

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    can u try this and see if it works:

    Code:
    fread(&Equip, sizeof(equipment), 1, fPtr);
    while ( !feof ( fPtr ) ) { 
       if ( Equip.eqNo != 0 )  { 
          printf("%-7d%-20s%-10.2f%-9d%-8d",Equip.eqNo, 
          Equip.eqName, Equip.price, Equip.stock, Equip.reorder); 
       } 
       fread(&Equip, sizeof(equipment), 1, fPtr); 
    }

  3. #3
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Exclamation fseek seems to do a neat trick...

    I see what you're saying, I haven't tried it yet but I'm going to in a short while. However, an interesting thing happened when I tried fseek inside the loop. Check this out...

    -----------------------------------------------------------------------

    x = 0;
    while(!feof(fPtr))
    {
    fseek(fPtr, (x) * sizeof(equipment), SEEK_SET);
    fread(&Equip, sizeof(equipment), 1, fPtr);
    if (Equip.eqNo != 0)
    {
    printf("%-7d%-20s%-10.2f%-9d%-8d",Equip.eqNo, Equip.eqName, Equip.price, Equip.stock, Equip.reorder);
    }
    x++;
    }

    -----------------------------------------------------------------------

    This solves the problem I was having earlier.

    However I am having an interesting issue. Sometimes when I store data in this structure, the reorder field converts its int value to a long string of numbers. Usually data entered in the reorder field is like "5", "10", "20" or something like that. But when you print out the information, it is sometimes something like "187273726392".

    Do you have any idea why this happens?
    Excuse me, while I water my money tree.

  4. #4
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Your code produces the same output

    When I tried your code, it produced the same output. It worked the first time. So I tried to input new data and it produced the same semi-gibberish output the second time around.

    Don't worry about that, the fseek seems to work ok. What I'm now having trouble with is the reorder field of the structure. It keeps changing the integer value stored in it to some long string of numbers. I capture data in the field like this:


    fflush(stdin);
    scanf("%d", &Equip.reorder);


    and like I had mentioned before, the data captured in this field is a small integer number like 5, 10, 15 or 20 etc. However, when you print out the records in the file, the reorder field for SOME of the records (not all) is a corrupted long string of integers. Any idea of why this happens?
    Excuse me, while I water my money tree.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You might try opening the file in binary mode, both when you write the file and read it.

    fPtr = fopen(filename, "rb+");

  6. #6
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Binary mode?

    Not to seem stubborn, but why should binary mode make a difference when I've used the same code (read, writing & printing code) for several other structs and random files and never came across this issue?

    Do you think it's because the last field is an int field?
    Excuse me, while I water my money tree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM