473,586 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("\nPleas e 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(lin e,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 9261
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("\nPleas e 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("\nPleas e 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(lin e,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(lin e,200,fd) != NULL)


Failure to check status return values is a mortal sin.

--
"If you want to post a followup via groups.google.c om, 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
12396
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() should be called during test.php's sleep, instead the first fgets blocks the processing of the parent script and do_smth_else is called only once....
4
10266
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 sure that there was a way around it but it escapes me if there is one. Thanks Charles Erwin
5
7230
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, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline...
20
7887
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 standard says about fgets: synopsis #include <stdio.h> char *fgets(char *s, int n, FILE *stream);
35
9939
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 the program. Since fgets() doesn't return the number of characters read it is pretty tough to handle the embedded nulls once they are in the buffer....
11
3654
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 EOF/error), returns n-1 characters plus a appended null character.
32
3862
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 default_buffer_length is changed from 4 --24 code works fine. But on the same line if I change the value of default_buffer_length from 4 --10 and I...
9
3413
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
8754
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/ specs gcc version 2.95.3 20010315 (release)
26
2253
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 string literal. Strlen was what I used to decide the \0 was not added. How can it be added for a string? My code. int main (void) { char input;...
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.