Linux learning miscellaneous _ file I/O Library _ Time Functions
Source: Internet
Author: User
Linux learning miscellaneous _ file I/O Library _ time function-Linux general technology-Linux programming and kernel information, the following is a detailed description. Text Files: ASCII or Unicode characters. Each ASCII character corresponds to one byte, ranging from 0 to 127, and the highest bit is always 0;
Binary files: they are stored in binary encoding;
# Include
FILE * fp;
FILE * fopen (const char * path, const char * mode); for example, fp = fopen ("file. a", "r"); if an error occurs, NULL is returned.
Meaning of mode
"R" read-only, must exist
"W" Write-only, re-create
"A" APPEND. If it does not exist, it is created.
"R +" read/write, must exist
"W +" read/write, re-create
"A +" read append. If it does not exist, it is created.
Standard input, standard output, FILE * pointers of standard error files are stdin, stdout, and stderr;
An important feature of UNIX: everything is a file;
Close FILE: int fclose (FILE * fp); close normally, return 0, non-zero is error;
Int fgetc (FILE * stream); Application: int ch = fgetc (fp); while (ch! = EOF );
Int fputc (int c, FILE * stream); write characters are returned normally; otherwise, EOF is returned; application: fputc (ch, fp );
Getchar () is equivalent to fgetc (stdin); putchar (ch) is equivalent to fputc (ch, stdout );
Exit (0); included in the library
File internal location pointer locating function:
Void rewind (FILE * fp); move the internal position pointer of the FILE referred to by fp to the FILE header
Long ftell (FILE * fp); returns the current position of the position pointer, that is, the number of bytes starting with the beginning of the FILE; returns-1 L if an error occurs during a call;
Int fseek (FILE * fp, long offset, int whence); where offset is positive, it indicates moving to the end of the FILE, and negative indicates moving to the FILE header;
How to move the whence position pointer
SEEK_SET moves the offset byte from the beginning of the file
SEEK_CUR moves the offset byte from the current position
SEEK_END move the offset byte from the end of the file
Char * fgets (char * s, int size, FILE * stream); s is the first address of the buffer; return value is s;
If the row is too long, read the size-1 character and add '\ 0' to the buffer. The remaining part will be read again;
If '\ n' or the end of the file has been read in size-1, it ends and' \ 0' is added at the end of the buffer ';
Int fputs (const char * s, FILE * stream); s is a string ending with '\ 0' and does not need to end with' \ n, the content written to the file does not include '\ 0 ';
--------------------------------------------------------------------------------
Examples of fgetc, fputc, fgets, and fputs:
/* Filename: fputs. c */
# Include
# Include
Int main ()
{
FILE * fp;
Int ch;
If (fp = fopen ("file2", "wt +") = NULL)
{
Printf ("Cannot open file \ n ");
Exit (1 );
}
Printf ("input a string: \ n ");
Ch = getchar ();
While (ch! = '\ N ')
{
Fputc (ch, fp );
Ch = getchar ();
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service