wrong reading or binary file

This is a discussion on wrong reading or binary file within the C Programming forums, part of the General Programming Boards category; Hello guys, this is my first post, and I'm not so good with English so be gentle please. Problem with ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    42

    wrong reading or binary file

    Hello guys,

    this is my first post, and I'm not so good with English so be gentle please.

    Problem with my code is that every time when function importData prints string 'series' it's smaller for one character. For example:

    AMSJETDZS
    MEKDIEJF
    SLEPDIE
    NFELDE

    and so on.

    Can anyone tell me what's wrong?



    Code:
    typedef struct{    char series[9+1];
        char origin[52+1];
        char quality;
        unsigned char eggNum;
    }Eggs;
    
    
    void importData(const char eggQual);
    
    
    int main()
    {
        char eggQual;
    
    
        do {
            printf("Enter quality of eggs: ");
            scanf("%c", &eggQual);
            getchar();
            if(eggQual<'A' || eggQual>'F')
                printf("Wrong input.\n");
        }while(eggQual<'A' || eggQual>'F');
    
    
        importData(eggQual);
    
    
        return 0;
    }
    void importData(const char eggQual)
    {
        Eggs j;
    
    
        FILE *fin=fopen("Jaja.dat", "rb");
        if(fin==NULL) exit(1);
    
    
        fread(&j, sizeof(Eggs), 1, fin);
        printf("%s\n", j.series);
        fread(&j, sizeof(Eggs), 1, fin);
        printf("%s\n", j.series);
        fread(&j, sizeof(Eggs), 1, fin);
        printf("%s\n", j.series);
        fread(&j, sizeof(Eggs), 1, fin);
        printf("%s\n", j.series);
        fread(&j, sizeof(Eggs), 1, fin);
        printf("%s\n", j.series);
        fread(&j, sizeof(Eggs), 1, fin);
        printf("%s\n", j.series);
    
    
    
    
        fclose(fin);
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,062
    Quote Originally Posted by yahzee View Post
    Problem with my code is that every time when function importData prints string 'series' it's smaller for one character. For example:

    AMSJETDZS
    MEKDIEJF
    SLEPDIE
    NFELDE

    and so on.
    Declaring your series as char series[9+1] means that the series has room for strings up to 9 characters in length, but they can also be shorter. Probably your data file has shorter strings in them, which is why they are shorter than 9 characters. Without seeing the datafile or how you generated it it'd be hard to say.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    I got it.
    origin has one character more than it should have. Now it works:

    Code:
    typedefstruct{    
          charseries[9+1];    
        char origin[51+1];
        char quality;
        unsigned char eggNum;
    }Eggs;
    Last edited by yahzee; 01-15-2013 at 05:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-28-2012, 08:16 AM
  2. Binary File Reading
    By Ludicrous in forum C Programming
    Replies: 5
    Last Post: 08-23-2012, 01:48 AM
  3. Reading a file in binary
    By maxsthekat in forum C++ Programming
    Replies: 7
    Last Post: 10-06-2009, 10:54 AM
  4. need Help! reading a binary file
    By d4rksid3 in forum C++ Programming
    Replies: 28
    Last Post: 10-17-2008, 03:37 PM
  5. Reading a binary file
    By Cyber Kitten in forum C Programming
    Replies: 2
    Last Post: 11-23-2001, 04:45 PM

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