Thread: C struct HELP

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    C struct HELP


    Please somebody help me, my C program its not working fine :(
    i wanna print the txt elements but nots working :(
    I wanna see this:
    111111
    2013
    01
    98
    etc...

    thanx


    #include <stdlib.h> // Fejlec (header) fajlok beemelese
    #include <stdio.h>

    #define Vege fflush(stdin); getchar(); return 0;



    struct eon {
    char oraszam[6];
    char ev[4];
    char ho[2];
    char fogyasztas[3];
    };

    int main(void)
    {
    struct eon villam;
    FILE *fpeon;


    fpeon = fopen("villen.txt", "rt");
    if(!fpeon)
    {
    printf("Fajl megnyitasi hiba.\n");
    Vege
    }

    while(1)
    {
    fread(&villam, sizeof villam, 1, fpeon);
    if(feof(fpeon))
    break;

    fgetc(fpeon);


    printf("%s\n",villam.oraszam);
    printf("%s\n",villam.ev);
    printf("%s\n",villam.ho);
    printf("%s\n",villam.fogyasztas);

    }



    Vege;
    }

    And the txt file:
    111111201301 98
    222222201301120
    333333201301240
    111111201302 99
    222222201302115
    333333201302232
    111111201303 86
    222222201303122
    333333201303230
    111111201304 95
    222222201304114
    333333201304235
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > fread(&villam, sizeof villam, 1, fpeon);
    You need to use fgets() to read a text file (one line at a time), then parse that line into your structure.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    For future note, you should be aware it is important that you indent your code suitably, and use [code] or [highlight] tags around your code samples. The forum software does not retain indentation by default, and in order for the code to be readable, you need to use the [code] tags. You can do this manually, or by using the '#' button at the top of the editing window and pasting your code into the edit box that comes up.

    C Code:
    #include <stdlib.h>                        // Fejlec (header) fajlok beemelese
    #include <stdio.h>
     
    #define  Vege    fflush(stdin); getchar(); return 0;
     
    struct eon
    {
        char      oraszam[6];
        char      ev[4];
        char      ho[2];
        char      fogyasztas[3];
    };
     
    int main(void)
    {
        struct eon villam;
        FILE     *fpeon;
     
     
        fpeon = fopen("villen.txt", "rt");
        if(!fpeon)
        {
            printf("Fajl megnyitasi hiba.\n");
            Vege
        }
     
        while(1)
        {
            fread(&villam, sizeof villam, 1, fpeon);
            if(feof(fpeon))
                break;
     
            fgetc(fpeon);
     
     
            printf("%s\n",villam.oraszam);
            printf("%s\n",villam.ev);
            printf("%s\n",villam.ho);
            printf("%s\n",villam.fogyasztas);
     
        }
     
        Vege;
    }


    In addition, I would recommend against the sort of macro you use here ('Vege'). Aside from the fact that it attempts to flush the stdin buffer - something which is non-portable, won't really work correctly, and strictly speaking shouldn't actually compile - it also inserts a return 0; without any indication that it is about to force the program closed. In a small program like this, it may seem harmless, but in a larger program it would be a potentially serious source of confusion.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    thankyou :cheers:
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    thanx


    Originally Posted by salem
    > fread(&villam, sizeof villam, 1, fpeon);
    You need to use fgets() to read a text file (one line at a time), then parse that line into your structure.
    thanx but i dont know how use in this example :(, i try it but i failed :(
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by geofrog
    thanx but i dont know how use in this example :(, i try it but i failed :(
    I have no idea what that is supposed to mean.

    Don't just say you tried something. Show us what you did. Only then could we ever possibly be able to help you with it.

    Please do not expect us to read your mind. We cannot do that.

IMN logo majestic logo threadwatch logo seochat tools logo