473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Jan 16 '06 #1
6 9235
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. */

}
Jan 16 '06 #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

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.

}

Jan 16 '06 #3
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)
Jan 16 '06 #4
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
es*****@acm-dot-org.invalid
Jan 16 '06 #5
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/>
Jan 16 '06 #6
Eric Sosman <es*****@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
Jan 16 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tanel | last post by:
Hello, I need to read a result of the first script (that takes some time to run) from the second script so that the first script doesn't block the second. Here is a short example. do_smth_else()...
4
by: Charles Erwin | last post by:
Is there any way, upon scanning in a file line by line to avoid missing the last line if there is not a newline character (aka you have to hit return on the last line of input in your file). I was...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
35
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in...
11
by: santosh | last post by:
Hi, A book that I'm currently using notes that the fgets() function does not return until Return is pressed or an EOF or other error is encountered. It then at most (in the absence of...
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
9
by: uidzer0 | last post by:
Hey everyone, Taken the following code; is there a "proper" or dynamic way to allocate the length of line? #include <stdio.h> #include <errno.h> int main(int argc, char **argv) { FILE *fp;
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
26
by: Bill Cunningham | last post by:
I was talking with someone about fgets and he said that fgets puts the \n in a string but not \0. I decided to test this assumption because my documentation didn't say if fgets put \0 after a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.