fgets

Discussion in 'C Programming' started by Harini, Jan 16, 2006.

  1. Harini

    Harini Guest

    Ive written this sample code in Dev C++ and use fgets to read from a
    external file. My compiler crashes with a windows error. Could somebody
    help me out

    a sample of the usage of of fgets in my code is listed below

    #include<stdio.h>
    #include<stdlib.h>
    #include <string.h>

    int main()
    {
    FILE *fd;
    char line[200];
    int count=0;
    char *tmp;
    int zip = 0;
    int uzip=0;

    printf("\nPlease enter your zip code : ");
    scanf("%d", &uzip);
    uzip = uzip/100;
    printf("uzip = %d",uzip);

    fd = fopen("c://Dev-Cpp/SA/STANDARD-SAMPLE1.csv","r");

    while(fgets(line,200,fd) != NULL)
    {
    tmp = strtok(line,",");
    zip = atoi(tmp);
    printf("zip=%d\n",zip);
    count++;
    }

    printf("# lines=%d",count);
    }


    thank you
    Harini, Jan 16, 2006
    #1
    1. Advertising

  2. Harini wrote:
    > Ive written this sample code in Dev C++ and use fgets to read from a
    > external file. My compiler crashes with a windows error. Could somebody
    > help me out


    You probably fail to open the file.
    >
    > a sample of the usage of of fgets in my code is listed below


    See the embedded comments:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int main()
    {
    FILE *fd;
    char line[200];
    int count = 0;
    char *tmp;
    int zip = 0;
    int uzip = 0;

    printf("\nPlease enter your zip code : ");
    scanf("%d", &uzip);

    /* mha: Please note that you need a call to fflush() after your
    prompt and that there is no reason to expect a US zip code to fit
    in an int */

    uzip = uzip / 100;
    printf("uzip = %d", uzip);

    fd = fopen("c://Dev-Cpp/SA/STANDARD-SAMPLE1.csv", "r");

    /* mha: Nowhere in your code do you check that the above succeeded.
    That means that it is quite possible that fd is NULL in the fgets
    call below. */

    while (fgets(line, 200, fd) != NULL) {
    /* mha: While the above may work fine, it is usually better to
    use something like 'sizeof line' rather than a hard-coded
    number like '200'.

    Others will disagree, but I find the superfluous comparison
    (!= NULL) a little jarring; those others will find leaving it
    out just as stylistically flawed. */
    tmp = strtok(line, ",");
    zip = atoi(tmp);
    /* mha: it is probably better to stick with the strto* family,
    since the ato* has much worse error-handling and does not give
    you a pointer to the end of the field. */

    printf("zip=%d\n", zip);
    count++;
    }

    printf("# lines=%d", count);
    /* mha: Even though C99 allows you to fall off the end of main, (1) I
    doubt you have a C99 compiler and (2) it is bad practice not to
    explicitly return values from functions that return values, as
    main does. */

    }
    Martin Ambuhl, Jan 16, 2006
    #2
    1. Advertising

  3. Harini wrote:
    > Ive written this sample code in Dev C++ and use fgets to read from a
    > external file. My compiler crashes with a windows error. Could somebody
    > help me out
    >
    > a sample of the usage of of fgets in my code is listed below
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    > #include <string.h>
    >
    > int main()
    > {
    > FILE *fd;
    > char line[200];
    > int count=0;
    > char *tmp;
    > int zip = 0;
    > int uzip=0;
    >
    > printf("\nPlease enter your zip code : ");
    > scanf("%d", &uzip);


    What if scanf fails ? Validate input.

    > uzip = uzip/100;
    > printf("uzip = %d",uzip);
    >
    > fd = fopen("c://Dev-Cpp/SA/STANDARD-SAMPLE1.csv","r");

    What if opening the file fails ? Please check.

    > while(fgets(line,200,fd) != NULL)
    > {
    > tmp = strtok(line,",");

    What if a comma isn't found ? Please check.

    > zip = atoi(tmp);

    What if conversion fails ?

    > printf("zip=%d\n",zip);
    > count++;
    > }
    >
    > printf("# lines=%d",count);


    main returns an int.


    > }
    >
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Jan 16, 2006
    #3
  4. Harini said:

    > Ive written this sample code in Dev C++ and use fgets to read from a
    > external file. My compiler crashes with a windows error. Could somebody
    > help me out


    A couple of obvious problems:

    Check that the file opened correctly. fopen will return NULL if it failed to
    open the file (which, looking at your filename, it just might).

    Check that strtok found a token before attempting to convert that token to a
    number. strtok will return NULL if it failed to find a token.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jan 16, 2006
    #4
  5. Harini

    Eric Sosman Guest

    Harini wrote:

    > Ive written this sample code in Dev C++ and use fgets to read from a
    > external file. My compiler crashes with a windows error. Could somebody
    > help me out


    The compiler should not crash, no matter what kind of code
    you feed it. Some code sequences (six million consecutive open
    parentheses, for example) may cause the compiler to exhaust the
    available resources or exceed internal limits, but even then a
    good compiler should terminate gracefully rather than crash ("with
    a windows error," whatever that might mean).

    You should complain to the makers of your compiler, or seek
    help from a forum devoted to that compiler. Malfunctions of the
    compiler are not C language issues.

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 16, 2006
    #5
  6. Harini

    Chuck F. Guest

    Harini wrote:
    >
    > Ive written this sample code in Dev C++ and use fgets to read
    > from a external file. My compiler crashes with a windows error.
    > Could somebody help me out
    >
    > a sample of the usage of of fgets in my code is listed below
    >

    .... snip code ...
    >
    > scanf("%d", &uzip);


    did scanf succeed?

    > uzip = uzip/100;
    > printf("uzip = %d",uzip);
    >
    > fd = fopen("c://Dev-Cpp/SA/STANDARD-SAMPLE1.csv","r");


    did fopen succeed?
    >
    > while(fgets(line,200,fd) != NULL)


    Failure to check status return values is a mortal sin.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Chuck F., Jan 16, 2006
    #6
  7. Harini

    Richard Bos Guest

    Eric Sosman <> wrote:

    > Harini wrote:
    >
    > > Ive written this sample code in Dev C++ and use fgets to read from a
    > > external file. My compiler crashes with a windows error. Could somebody
    > > help me out

    >
    > The compiler should not crash, no matter what kind of code
    > you feed it. Some code sequences (six million consecutive open
    > parentheses, for example) may cause the compiler to exhaust the
    > available resources or exceed internal limits, but even then a
    > good compiler should terminate gracefully rather than crash ("with
    > a windows error," whatever that might mean).
    >
    > You should complain to the makers of your compiler,


    Well, yeah, except that the OP probably does not describe what happens
    accurately. At least on my system, Dev-C++ does _not_ crash when
    compiling that program, but the resulting executable does (for the
    reason others have already indicated, I think).

    Richard
    Richard Bos, Jan 16, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles Wilkins

    cin before fgets

    Charles Wilkins, Aug 7, 2003, in forum: C++
    Replies:
    4
    Views:
    694
    Charles Wilkins
    Aug 7, 2003
  2. Mike Mimic

    fgets and newline

    Mike Mimic, May 15, 2004, in forum: C++
    Replies:
    4
    Views:
    7,980
    John Harrison
    May 15, 2004
  3. DJP
    Replies:
    7
    Views:
    7,348
    glen herrmannsfeldt
    Oct 21, 2004
  4. Replies:
    6
    Views:
    1,223
    Mark McIntyre
    Jun 25, 2003
  5. Eigenvector

    fgets and problems reading into array

    Eigenvector, Jul 26, 2003, in forum: C Programming
    Replies:
    12
    Views:
    615
    Glen Herrmannsfeldt
    Jul 29, 2003
Loading...

Share This Page