Re: [Dev-C++] Reading file with getc(fp)
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Luigi S. <gi...@li...> - 2002-04-10 20:54:37
|
At 18:47 09/04/02 -0700, rchavez wrote:
>Hi,
>
>I am trying to read a text file in a C program. This code runs fine on the
>unix C environments I can access but gives a fatal error that removes the
>console window in Dev-C++ environment.
I have attempted to run you code and the same thing happened to me, I think
the reason is: when the file named "word.h" does not exist, fp is assigned
a NULL value, so your code attempts to read something using a NULL pointer,
as a result, a run time error occurs.
>There are no warnings at compile time.
I shall not agree, in fact I tried to compile your code (using Dev-C++ 4.0
with MinGW 2.95) after setting these additional options:
-Wall -Wnested-externs -Wredundant-decls
Here is what I get in the "Compiler" panel:
4 c:\mail\rob\orig.c warning: return-type defaults to `int'
7 c:\mail\rob\orig.c warning: nested extern declaration of `fopen'
7 c:\mail\rob\orig.c warning: redundant redeclaration of `fopen' in same scope
179 c:\progra~2\dev-c_~1\include\stdio.h warning: previous declaration of
`fopen'
13 c:\mail\rob\orig.c warning: implicit declaration of function `system'
C:\mail\rob\orig.c compiled successfully
As you can see these are only warnings, so code is generated at your own
risk! :)
Anyway, at compile time one can't catch run time errors.
> It is definitely the getc() that causes the trouble because the program
>works in Dev-C++ when I use getch() and take input from standard input.
I do not know what the function "getch()" does, one thing I'm sure of, is
that it is not ANSI C 89.
> Four questions:
>
>1. How do I make it work with Dev-C++ ?
Try the code I wrote below, it works on my machine (Win98SE, Dev-C++ 4.0,
MinGW 2.95).
Note, if you are using my same version of Dev-C++, make sure that:
"Options"->"Compiler Options"->"Linker"->"compile for Win32 (no console)"
is unset, otherwise you will see nothing displayed on the screen.
>2. There was lots of talk on getch() and getchar() in the archives but
>nothing on getc(). Is getc() no longer ANSI C ?
Don't know what ANSI C99 says, however, since "getc" is in C89, I suppose
it is still supported
>3. getch() swallows the cntrl-Z so I can't properly terminate using std
>input. How do I overcome this when I want to use getch()?
As I said above, getch() is not a standard function.
>4. Why is EOF cntrl-Z instead of cntrl-D in Dev-C?
This is not related to Dev, it has to do with the differences between Dos
and Unix.
Since Dos came after Unix, I see no reason why to assume different
conventions (another one I don't understand is the backslash and the slash
issue).
>Your help will be greatly appreciated
hope it solves the problem
>and you people are wonderful to give so much help to the clueless like me
>on this list!
>Rob.
>
>----------------- my code --------------------------
>
>#include <stdio.h>
>
>main()
>{
>
>int c;
>FILE *fopen(), *fp;
>
>fp = fopen("words.h","r");
>while ( (c = getc(fp)) != EOF )
> printf("\ngot a char %c",c);
>
> system("PAUSE"); /** added this for Dev-C++ **/
> return 0;
>}
You need to add no special line of code if you run console applications
from the ... console! :)
Also remember that using function "PAUSE" makes program not portable to
Unix (where there is no command named "PAUSE").
/* file test.c */
#include <stdio.h>
#include <errno.h>
#define FILENAME "words.h" /* name of file to be read */
int
main()
{
FILE* fp = fopen(FILENAME,"r"); /* the file */
if (fp == NULL) /* unable to open file */
perror(FILENAME); /* notify user */
else /* ok, file is open */
{
int c; /* the character read from the file */
while ((c = getc(fp)) != EOF) /* found a character */
printf("\ngot a char %c", c); /* display the character */
}
return 0;
}
--
Luigi
gi...@li...
|