Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   fgets (http://www.velocityreviews.com/forums/t440912-fgets.html)

Harini 01-16-2006 07:06 AM

fgets
 
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


Martin Ambuhl 01-16-2006 07:32 AM

Re: fgets
 
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. */

}

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?= 01-16-2006 10:24 AM

Re: fgets
 
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.


> }
>


Richard Heathfield 01-16-2006 11:47 AM

Re: fgets
 
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)

Eric Sosman 01-16-2006 02:29 PM

Re: fgets
 
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
esosman@acm-dot-org.invalid

Chuck F. 01-16-2006 04:04 PM

Re: fgets
 
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/>

Richard Bos 01-16-2006 05:13 PM

Re: fgets
 
Eric Sosman <esosman@acm-dot-org.invalid> 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


All times are GMT. The time now is 10:52 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.