When fopen fails,
Getch ();
Exit (1 );
The compilation fails, prompting that the two do not exist...
Man.
# Include <curses. h> This includes getch (), wgetch (), and so on...
# Include <stdlib. h> This includes the exit (INT value) function...
After that, another problem occurs:
[Ktktkt @ Jintao cprogram] $ GCC 1.C-O 1-wall
1. C: In function 'main ':
1. C: 9: Warning: Assignment makes pointer from integer without a cast
/Tmp/cc0cwbi1. O (. Text + 0x4c): In function 'main ':
: Undefined reference to 'stdscr'
/Tmp/cc0cwbi1. O (. Text + 0x51): In function 'main ':
: Undefined reference to 'wgetch'
Collect2: LD returned 1 exit status
I cannot understand this... stdscr does not have man-related introduction. And I do not know what these compilation errors mean...
It seems that stdscr is used by getch...
This compilation fails. In the cause search, Echo $? In bash? Also return 1, it seems there is an error... it would be nice if you have a friend who gives directions :)
Code:
# Include <stdio. h>
# Include <curses. h>
# Include <stdlib. h>
Int main ()
{
File * FP;
If (FP = fopen ("/usr/include/stdio. H", "R") = NULL)
{
Printf ("/nerror on open stdio. h/N ");
Getch ();
Exit (1 );
}
Fclose (FP );
Return 0;
}
It turned out to be like this:
# GCC 1.C-O 1-wall-lcurses
You need to use the curses library. Although the header file contains only the function name, the header file does not implement the function (getch (). Therefore, you must connect to the library...
After reading this, whereis was used to find the library.
[Ktktkt @ Jintao file] $ whereis libcurses
Libcurses:/usr/lib/libcurses. So/usr/lib/libcurses.
There seems to be another static Link Library ~
Well, the whole code for Directly Reading a file is stupid. One character and one character can be read.
# Include <stdio. h>
# Include <stdlib. h>
# Include <curses. h>
Int main ()
{
Char C;
File * FP;
If (FP = fopen ("file", "R") = NULL)
{
Printf ("cannot open the file, please press any key to exit/N");
Getch ();
Exit (1 );
}
C = fgetc (FP );
While (C! = EOF)
{
Putchar (C );
C = fgetc (FP );
}
Fclose (FP );
Return 0;
}