Read struct-like file/print to stdout (again)

This is a discussion on Read struct-like file/print to stdout (again) within the C Programming forums, part of the General Programming Boards category; Hello again, Thanks to Salem for the previous help, but I still can't seem to work this out... I apologize ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    6

    Read struct-like file/print to stdout (again)

    Hello again,
    Thanks to Salem for the previous help, but I still can't seem to work this out...
    I apologize to all.

    Before I had asked to read many different struct-like records, dynamically allocate memory for each and then print them to stdout, but it would seem something for linked-lists and I'd prefer to avoid this. So it would be simpler to just read only *one* struct-like info file and then print to stdout.

    Here's the file to read from disk, called "struct holidays":

    {1, 15, "Seijin no hi (Coming of Age Day)"},
    {2, 11, "Kenkoku Kinenbi (National Foundation Day)"},
    {3, 20, "Shunbun no hi (Equinox Day)"},
    {4, 29, "Midori no hi (Greenery Day)"},
    {5, 3, "Kenpou kinenbi (Constitution Day)"},
    {6, 20, "Chi-chi no hi* (Father's Day, third Sunday in June)"},
    {7, 25,"Umi no hi (Sea/Marine Day)"},
    {8, 15, "Shuusen Kinenbi* (End of WWII)"},
    {9, 15, "Keiro no hi (Senior's Day)"},
    {10, 10, "Taiku no hi (Physical Education Day)"},
    {11, 3, "Bunka no hi (Culture Day)"},
    {12, 23, "Tennou Tanjoubi (Emperor's Birthday)"}

    Here is my code, which covers two requirements;
    1. use malloc or calloc (or realloc if necessary),
    2. use the arrow operator to access the infile data.

    struct holidays *readIn;
    FILE *pHolidays;
    int count = 0;

    int size = sizeof (struct holidays);

    readIn = (struct holidays *) malloc (sizeof (struct holidays));

    if ((pHolidays = fopen ("InputFile", "rb")) == NULL)
    {
    fputs ("\nUnable to open InputFile", stderr);
    exit (1);
    }

    while (fread (readIn, size, 1, pHolidays) == 1)
    {

    printf ("\n %d %d %s", readIn->month, readIn->day, readIn->holidayName);
    count++; //appears rather useless here I realize...

    }

    The ouput usually ends up reading from lines 3, 6, 8 or 9 of the input file and prints garbage chars for the end of the holiday names list.

    Thank you for reading this and for any and all previous help, as well as the professionalism and courtesy that is always received...

    inakappeh

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    6

    Reply to Salem for reading structs fr infile

    Hi Salem
    Thank you again for your reply. I don't have the zip software anymore but infile is very small so I'll just include it as a text file. The rest of the program does unrelated things so the function that is to read the infile is a kind of stand-alone function, as in main () it just appears as a simple call. In its entirety it is:
    /* ---------------------------------------------------------------------
    FileIn ();
    Pre:
    Post:
    ---------------------------------------------------------------------- */
    int FileIn (void)
    {

    struct holidays *readIn;
    FILE *pHolidays;
    int i = 0;

    int size = sizeof (struct holidays);


    readIn = (struct holidays *) malloc (sizeof (struct holidays));

    if ((pHolidays = fopen ("KrazyFile.txt", "rb")) == NULL)
    {
    fputs ("\nUnable to open KrazyFile", stderr);
    exit (1);
    }

    while (i < 12 && fread (readIn, size, 1, pHolidays) == 1)
    {

    printf ("\n %d %d %s", readIn->month + i, readIn->day + i, readIn->holidayName + i);
    readIn++;
    i++; //probably won't work...
    }

    //fwrite (readIn, size, 12, stdout); //gave up with this...

    return 0;
    }

    Thank you very much again. I don't know why this has me so flummoxed...

    inakappeh
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi
    You made a wrong assumption that your data in the file is binary and you process it as that kind. But unfortunately the data is a thet so you should convert it before assign the values to data members.
    In your case instead of fread() try to use fscanf with proper format string to arheive you goal.

    damyan

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    ... Okay this is really strange. Your file is C syntax which... either you need to do some parsing, which really isn't an especially thrilling prospect, or you can try this...

    Code:
    #include <stdio.h>
    main ()
    {
     file * f; 
     #include krazyfile.txt
    
     f = fopen ("krazyfile2.txt", "w");
     fwrite (USA, sizeof (struct holidays), 12, f);
     return;
    }
    Then try opening krazyfile2.txt from your program instead of krazyfile.txt. All this does is changes what you've got in krazyfile into a binary representation of the array. Of course, this program will only use the version of krazyfile.txt that's available at compile time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

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