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 ();

}
Rewind (fp );
Ch = fgetc (fp );
While (ch! = EOF)
{
Putchar (ch );
Ch = fgetc (fp );

}
Printf ("\ n ");
Fclose (fp );
Return 0;
}

/* Fcopys. c */

# Include
# Include

Int main (int argc, char * argv [])
{
FILE * fp1, * fp2;
Char * ch;
Char a [100];

If (argc! = 3)
{
Printf ("usage: command file1 file2 \ n ");
Exit (0 );
}
If (fp1 = fopen (argv [1], "rt") = NULL)
{
Printf ("Cannot open % s \ n", argv [1]);
Exit (1 );
}
If (fp2 = fopen (argv [2], "wt +") = NULL)
{
Printf ("Cannot open % s \ n", argv [2]);
Exit (1 );
}
// Ch = fgets (a, 100, fp1 );
While (fgets (a, 100, fp1 )! = NULL)
{
Fputs (a, fp2 );
}
Fclose (fp1 );
Fclose (fp2 );
Return 0;
}

/* Fcopy. c */

# Include
# Include

Int main (int argc, char * argv [])
{
FILE * fp1, * fp2;
Int ch;

If (argc! = 3)
{
Printf ("usage: command file1 file2 \ n ");
Exit (0 );
}
If (fp1 = fopen (argv [1], "rt") = NULL)
{
Printf ("Cannot open % s \ n", argv [1]);
Exit (1 );
}
If (fp2 = fopen (argv [2], "wt +") = NULL)
{
Printf ("Cannot open % s \ n", argv [2]);
Exit (1 );
}
While (ch = fgetc (fp1 ))! = EOF)
Fputc (ch, fp2 );
Fclose (fp1 );
Fclose (fp2 );
Return 0;
}

----------------------
Use of time functions:
# Include
# Include
# Include

Int main ()
{
FILE * fp;
Int n = 0;
Int tmpsec = 0;
Struct tm * t;
Time_t var;
Char s [50];

Time (& var );
T = localtime (& var );
If (fp = fopen ("timerecord", "at +") = NULL)
{
Printf ("open file failed! \ N ");
Exit (1 );
}

If (! Feof (fp ))
{
While (fgets (s, 50, fp ))
N ++;
}
While (n <100)
{
If (tmpsec! = T-> tm_sec)
{
Tmpsec = t-> tm_sec;
N ++;

Sprintf (s, "% d, % d-% d: % d \ n", n ,\
1900 + t-> tm_year, 1 + t-> tm_mon, t-> tm_mday, t-> tm_hour, t-> tm_min, t-> tm_sec );
Puts (s );
Fputs (s, fp );
Fflush (fp );
}
Time (& var );
T = localtime (& var );
}
Fclose (fp );
}

The result is as follows:
1, 14:41:38
2, 14:41:39
3, 14:41:40
4, 14:41:41
5, 14:41:42
6, 14:41:43

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.