[aftertaste C] input/output

Keywords: REST C

flow

In C language, stream represents the source of arbitrary input or the destination of arbitrary output.

field name pointer

Access to streams in C programs is achieved through file pointers. The type of this pointer is FILE* (in

Standard stream and redirection

Text files and binary files

In text files, bytes represent characters, which allows people to check or edit files.
In binary files, bytes do not necessarily represent characters; byte groups can also represent other types of data, such as integers and floating-point numbers.

Text files have two features that binary files do not have:
1. The text file is divided into several lines.
2. Text files can contain a special "end of file" tag.

===============================================

File operation

Simplicity is one of the charms of input and output redirection: you don't need to open files, close files, or perform any other display file operation.

Open file

FILE *fopen(const char * restrict filename, const char * restrict mode);
If you want to use a file as a stream, you need to call the fopen function when you open it.
Rest is the C99 keyword, which means that the memory unit of the string pointed to by filename is not shared.

Pattern

Close file

int fclose(FILE *stream);

Additional files for open streams

FILE *Freopen (const char * restrict filename, const chat * restrict mode, FILE * restrict stream);

Temporary file

FILE *tmpfile(void);
char *tmpnam(char *s);

The tmpfile function creates a temporary file that will always exist unless it is closed or the program terminates.
The call to the tmpfile function returns a file pointer that can be used to access the file later.
If the file creation fails, the tmpfile function returns a null pointer.

Although the tmpfile function is easy to use, it has two drawbacks:
1. It is impossible to know what the filename created by the tmpfile function is.
2. Documents cannot be made permanent by subsequent decisions.

File buffer

int fflush(FILE *stream);
void setbuf(FILE * restrict stream, char * restrict buf);
int setvbuf(FILE * restrict stream, char * restrict buf, int mode, size_t size);

Passing data to or from a disk drive is a relatively slow operation. Therefore, it is not feasible to access disk files directly every time a program wants to read or write characters. The key to better performance is buffering.

Store the data of the write stream in an internal buffer area; when the buffer is full (or the stream is closed), clean the buffer (write to the actual output device).
Input streams can be buffered in a similar way: buffers contain data from the input device and read data from the buffer rather than from the device itself.

Other file operations

int remove(const char *filename);
int remove(const char *old, const char *new);

The remove and rename functions allow the program to perform basic file management operations.
The remove function and rename function process the file name instead of the file pointer.
If the call is successful, both functions return zero; otherwise, both return non-zero values.

===============================================

C99 has made many modifications to the conversion instructions of printf function and fprintf function.
1. Added length modifier.
2. Adding conversion descriptor.
3. Allow the output of infinite trees and NaN.
4. Expenditure wide characters. Five
5. The previously undefined transformation instructions are now allowed.

Detect end of file and error condition

void clearerr(FILE *stream);
int feof(FILE *stream);
int ferror(FILE *stream);

===============================================

Character input/output

Output function

int fputc(int c , FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);

Input function

int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar(void);
int ungetc(int c, FILE *stream);

===============================================

Line input/output

Output function

int fputs(const char * restricrt s , FILE * restrict stream);
int puts(const char *s);

Input function

char *fgets(char * restrict, int n, FILE * restrict stream);
char *gets(char *s);

===============================================

Input/Output of Blocks

size_t fread(void * restrict ptr,
             size_t size, size_t nmemb,
             FILE * restrict stream);

size_t fwrite(const void * restrict ptr,
              size_t size, size_t nmemb,
              FILE * restrict stream);

===============================================

File location

int fgetpos(FILE * restrict stream, fpos_t * restrict pos);
int fseek (FILE *stream, long int offset, int whence);
int fsetpos(FILE *stream, const fpos_t *pos);
long int ftell(FILE *stream);
void rewind(FILE *stream);

===============================================

Input/Output of Strings

Output function

int sprintf(char * restrict s, const char * restrict format , ...);
int snprintf(char *restrict s, size_t n, ... const char * restrict format , ...);

Input function

int sscanf(const char * restrict s, const chat * restrict format, ...)

Posted by cocpg on Fri, 17 May 2019 04:53:51 -0700