Reading from an data?

This is a discussion on Reading from an data? within the C Programming forums, part of the General Programming Boards category; The data is... Code: John Everhart 24900. 1 car Paul Evans 95700. 4 cars Juan Hernandez 45100. 2 cars My ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    24

    Reading from an data?

    The data is...
    Code:
    John Everhart 24900.
    1 car
    Paul Evans 95700.
    4 cars
    Juan Hernandez 45100.
    2 cars
    My code...
    Code:
    #include <stdio.h>
    #include <math.h>
    #define FILENAME "input.dat"
    
    main()
    {
    FILE *fin, *fout;
    char text[81], text2[81], first[40], last[40], sale[40], car[40];
    int lines;
    double number;
    
    fin = fopen("/home/hp/iacs5/me9x/public/input.dat", "r");
    fout = fopen("output.dat", "w");
    if((fin == NULL) || (fout == NULL))
    {
    printf("file open failure\n");
    exit(1);
    }
    
    lines=0;
    printf("\n");
    fprintf(fout, "\n");
    
    while(fgets(text,81,fin)!=NULL)
    {lines++;
    if (lines%2 != 0)
    {
    sscanf(text, "%s %s %s", first, last, sale);
    printf("Mr/Ms %s's sale was %s\n", last, sale);
    }
    
    else
    {
    sscanf(text2, "%s %s", number, car);
    printf("His/Her commision is %s\n", number);
    }
    
    }
    }
    The Mr/Ms %s's sale prints out okay for the first line, then I get a core dump. The first part and the second part looks exactly like the same in the code, so why doesn't it work?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're trying to read a double with %s.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    24
    Sorry, but I changed number to integer, and it still gave a core dump.

    The output should be like...
    Mr/Ms Everhart's sale was 24900.00
    His/Her commission is = 24900.00

  4. #4
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    You were close, the changes I made are in red and read the comments I added.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        FILE *fin, *fout;
        char text[81], text2[81], first[40], last[40], sale[40], car[40];
        int lines;
        int number;    //no one ever has 4.3798 cars, this only needs to be an integer.
    
        fin = fopen("/home/hp/iacs5/me9x/public/input.dat", "r");
        fout = fopen("output.dat", "w");
        
        if ( (fin == NULL) || (fout == NULL))
        {
             printf("file open failure\n");
             exit(1);
        }
    
        lines = 0;
        printf("\n");
        fprintf(fout, "\n");
    
        while( fgets( text, 81, fin ) != NULL )
        {
               lines++;
               
               if ( lines % 2 != 0 )
               {
                    sscanf(text, "%s%s%s", first, last, sale);    //no need for spaces between each %s.
                    printf("Mr/Ms %s's sale was %s\n", last, sale);
               }
               else
               {
                   sscanf(text, "%d%s", &number, car);    //use %d for integer and don't forget to use an ampersand!
                                                                Also, fgets is only ever reading data into text, not text2 as you had here previously
                   printf("His/Her commision is %d\n", number);    //again, %d for integer
               }
    
        }
        
        getchar();
        return 0;
    }
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    24
    Wow, much thanks man. It's the little things that mess me up.

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    I find that whenever a program segfaults (crashes) on me, I look straight to the (s/f)scanf() lines for missing ampersands.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > Wow, much thanks man. It's the little things that mess me up
    > I look straight to the (s/f)scanf() lines for missing ampersands.
    You people need (NEED) to discover the magic of gcc options

    Code:
    gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c:6: warning: return type defaults to `int'
    foo.c: In function `main':
    foo.c:17: warning: implicit declaration of function `exit'
    foo.c:34: warning: format argument is not a pointer (arg 3)
    foo.c:35: warning: format argument is not a pointer (arg 2)
    foo.c:39: warning: control reaches end of non-void function
    foo.c:10: warning: 'number' might be used uninitialized in this function
    It will tell you when you mess up your printf and scanf calls long before you're staring down the barrel of a segfault.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    >You people need (NEED) to discover the magic of gcc options

    Care to explain that for someone who has no idea what they're doing when it comes to the finer details of debugging?

    I'm using dev-c++ on winxp if it makes a difference.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    gcc -W -Wall -ansi -pedantic -O2 foo.c
    All explained here

    They're GCC commandline options, and will work wherever you install GCC in. Windows, *nix, your microwave oven processor, etc.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Yes, that's all well and good. But does this mean I have to compile/debug all my projects from the console now?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    You should have no trouble at all getting Dev-C++ to run extra compiler switches. In Tools there are Compiler options, which will bring up a dialogue box. Type.

    It's magic!
    Attached Images Attached Images  

  12. #12
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Ah, magic indeed. Cheers mate.

    Though, I don't think I should be adding 'foo.c'
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading large complicated data files
    By dodzy in forum C Programming
    Replies: 16
    Last Post: 05-17-2006, 05:57 PM
  2. accessing my com port, writing and reading data
    By shoobsie in forum C Programming
    Replies: 7
    Last Post: 09-16-2005, 04:29 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM
  4. reading data format into program
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 10-23-2003, 03:27 PM
  5. Binary data reading
    By jdinger in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2002, 06:56 PM

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