fgets

H

Harini

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
 
M

Martin Ambuhl

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

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?=

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 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.
 
R

Richard Heathfield

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.
 
E

Eric Sosman

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

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.
 
C

Chuck F.

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 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/>
 
R

Richard Bos

Eric Sosman said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top