program crashes + fscanf() question

This is a discussion on program crashes + fscanf() question within the C Programming forums, part of the General Programming Boards category; A struct is a blueprint - it is not an instance. In essence, it is not something that actually exists! ...

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    A struct is a blueprint - it is not an instance. In essence, it is not something that actually exists! You must construct something from it first, to be able to manipulate its members.
    That is why the compiler barks-
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    If I was to use this to read the month, day, year, hour and minutes, it works:

    Code:
     struct data {
            int month;
            int day;
            int year;
            int hour;
            int minute;
        } LineData;
    It's when I try to read the strings(using pointers and malloc in the structure), the compiler chokes.

    EDIT: To recap, I am trying to open up and read a file that contains the following line:

    12/16/2008 10:21 AM 969 happyclown.html
    OS: Linux Mint 13(Maya) LTS 64 bit.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    As I told you, the struct is a blueprint; you cannot modify its contents. You must do it on a instance - a creation from the blueprint, in this case, LineData.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by Elysia View Post
    As I told you, the struct is a blueprint; you cannot modify its contents. You must do it on a instance - a creation from the blueprint, in this case, LineData.
    *scratches head*

    What do you mean modify it's contents?

    Do you mean by trying to allocate memory to pointers in the structures, I am modifying the content of the struct?

    Anyway, this works for reading the days and times:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
        FILE *openfile;
    
        struct data {
            int month;
            int day;
            int year;
            int hour;
            int minute;
        } LineData;
    
        if((openfile = fopen( "directorylisting.txt", "rt" )) == NULL)
        {
            perror("Error opening file");
            exit(EXIT_FAILURE);
        }
    
        fscanf(openfile, "%d/%d/%d %d:%d", &LineData.month, &LineData.day, &LineData.year, &LineData.hour,
                                        &LineData.minute);
    
        fclose(openfile);
    
        printf("%d/%d/%d %d:%d", LineData.month, LineData.day, LineData.year, LineData.hour, LineData.minute);
    
        return 0;
    }
    OS: Linux Mint 13(Maya) LTS 64 bit.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Code:
        struct data {
            int month;
            int day;
            int year;
            int hour;
            int minute = 10;
        } LineData;
    Bad, because it tries to modify (assign) 10 to minute inside the struct declaration.
    Code:
        struct data {
            int month;
            int day;
            int year;
            int hour;
            int minute;
        } LineData;
    LineData.minute = 10;
    OK, because we are modifying the instance of the data struct, and not the struct (blueprint) itself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Ok, I think I got you now. So I shoud do this?
    Code:
     struct data {
            int month;
            int day;
            int year;
            int hour;
            int minute;
            char *string1;
            int number;
            char *string2;
        } LineData;
    LineData.string1 = malloc( 10 * sizeof(char));
    LineData.string2 = malloc( 30 * sizeof(char));
    OS: Linux Mint 13(Maya) LTS 64 bit.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Yes, that will work. Just don't forget to free them later.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Well, this code compiles fine, but it only reads in the first 2 colums(mon/day/year hour/min), and doesn't read the rest.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
        FILE *openfile;
    
        struct data {
            int month;
            int day;
            int year;
            int hour;
            int minute;
            char *string1;
            int number;
            char *string2;
        } LineData;
    
        LineData.string1 = malloc( 10 * sizeof(char));
        LineData.string2 = malloc( 30 * sizeof(char));
    
        if((openfile = fopen( "directorylisting.txt", "rt" )) == NULL)
        {
            perror("Error opening file");
            exit(EXIT_FAILURE);
        }
    
        fscanf(openfile, "%d/%d/%d %d:%d %s %d %s", &LineData.month,
    &LineData.day, &LineData.year, &LineData.hour, &LineData.minute,
     LineData.string1, &LineData.number, LineData.string2);
    
        fclose(openfile);
    
        printf("%d/%d/%d %d:%d %s %d %s", LineData.month, 
    LineData.day, LineData.year, LineData.hour, LineData.minute,
    *LineData.string1, LineData.number, *LineData.string2);
    
        return 0;
    }
    At least the compiler is happy. Thanks Elysia.

    Time for me to go to sleep! Zzzzzzzzzzz....
    OS: Linux Mint 13(Maya) LTS 64 bit.

  9. #24
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Actually after I removed the mysterious asterisks in the printf statement:
    Code:
         printf("%d/%d/%d %d:%d %s %d %s", LineData.month, 
    LineData.day, LineData.year, LineData.hour, LineData.minute,
    *LineData.string1, LineData.number, *LineData.string2);
    This program ran fine and the output was:
    12/16/2008 10:21 AM 969 happyclown.html

    One point I would make is that it's silly to malloc a value if you are certain what it is going to be and it is not going to change. Rather than use pointers in your struct prototype, just do this:
    Code:
        struct data {
            int month;
            int day;
            int year;
            int hour;
            int minute;
            char string1[10];
            int number;
            char string2[30];
        } LineData;
    Since string1 and string2 are part of the struct, they will be part of the stack (memory) when LineData is declared.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    The printf specifier expects a pointer to char (char*) of the string to print. But you dereference the pointer and thus get the char it points to, giving you undefined output.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    The printf specifier expects a pointer to char (char*) of the string to print. But you dereference the pointer and thus get the char it points to, giving you undefined output.
    What Elysia means is that there is a difference between declaring a pointer (char *ptr) and using it (ptr), whereby *ptr would be dereferencing. In this case "undefined output" will probably be:

    Segmentation Fault
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Or access violation!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    I am happy the program finally works.

    Thanks for everybody's help.
    OS: Linux Mint 13(Maya) LTS 64 bit.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Replies: 4
    Last Post: 06-23-2006, 08:03 PM
  3. DEV-C++ made program crashes when run from outside the ide
    By rainmanddw in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 10:27 PM
  4. Question about K&R program
    By Aerie in forum C Programming
    Replies: 15
    Last Post: 04-24-2005, 08:09 AM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 06:10 AM

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