Why does an integer print like that?

This is a discussion on Why does an integer print like that? within the C Programming forums, part of the General Programming Boards category; I have a structure defined as such: typedef struct { int eqNo; char eqName[20]; float price; int stock; int reorder; ...

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

    Question Why does an integer print like that?

    I have a structure defined as such:


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


    and I capture the data for each field like this:


    fflush(stdin);
    gets(Equip.eqName);
    fflush(stdin);
    scanf("%f", &Equip.price);
    fflush(stdin);
    scanf("%d", &Equip.stock);
    fflush(stdin);
    scanf("%d", &Equip.reorder);


    However, when I print out this information, the last field, reorder, prints out a long string of integers instead of the value actually placed in it. This only happens for some of the records.

    Has anyone ever experienced this issue? If so, could you kindly enlighten me please? Thanks.
    Excuse me, while I water my money tree.

  2. #2
    Temfate
    Guest
    Try this instead:

    fflush(stdin);
    gets(Equip.eqName);
    fflush(stdin);
    scanf(" %f", &Equip.price);
    fflush(stdin);
    scanf(" %d", &Equip.stock);
    fflush(stdin);
    scanf(" %d", &Equip.reorder);

    Other than that; I'd have to think for awhile...

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Don't use gets, use fgets instead since you can limit how many characters get copied into your eqName buffer. This will eliminate the chance that some idiot types in a name longer that the space available and winds up overwriting some memory location some where.

    As for your question, how are printing out the data. Show us that code as well since it could be a piece of the puzzle.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70
    hk_mp5pdw - the printing source code :


    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++;
    }


    Let me explain a few things here. The file is written using the following code:


    fseek(fPtr, (Equip.eqNo - 1) * sizeof(equipment), SEEK_SET);
    fwrite(&Equip, sizeof(equipment), 1, fPtr);


    I have created no sophisticated hashing algorithm to determine a random position in the random file to store the actual data, so I simply used Equip.eqNo - 1 to do that.

    The user enters a number for the eqNo field. (a simple int between 1-100, as the file can store a max of 100 records)

    The reorder field is however, also an int. But what I notice is that for each subsequent record written, the reorder field for the previous record gets corrupted.

    I hope this makes sense. Do you know what could be happening here?
    Excuse me, while I water my money tree.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >scanf("%d", &Equip.reorder);
    I would add a printf() here to see if reorder got stored.

    Also, did you open the file in binary mode when you wrote it and read it back?
    fopen(filename,"wb");

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

    Lightbulb It works at home!

    The strangest thing, when I take the program home, it works beautifully. No idea why.

    I'm running Win NT 4 on NTFS 4 at work.
    At home I'm running Windows 2000 with FAT 32.

    I wonder if that has anything to do with it.
    Excuse me, while I water my money tree.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    that could well be the problem, The architecture of each system. You are using 32bit at home and possible 16bit at work.

    to resolve the problem copy your file at home from binary to text file. Small self written program to do this. Reverse process at work so that the binary file created is in the correct architecture.

    while(feof)
    fread(your structure, binfp)

    fprintf(textfp, your structure variables)
    /* to write to text reverse to write binary*/
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Replies: 7
    Last Post: 08-19-2007, 09:10 AM
  3. Putchar() to print an integer value?
    By swgh in forum C Programming
    Replies: 2
    Last Post: 05-19-2007, 01:30 PM
  4. Replies: 1
    Last Post: 07-31-2002, 12:35 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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