当前位置:网站首页>Lei niukesi --- basis of embedded AI

Lei niukesi --- basis of embedded AI

2022-06-21 09:09:00 Renax AI (Linux AI)

Linux file IO( standard I/O< One >)

– One 、 standard IO

– Two 、 flow [FILE]

– 3、 ... and 、 Opening and closing of flow

– Four 、 Read write flow

One 、 standard IO

  • In a word, understand : Using standard C Write out a set of with input and output API( Application programming interface ) function
  • You have to know Two nouns : Buffer mechanism 、 system call .
  • standard IO adopt The buffer mechanism reduces the number of system calls So as to achieve more efficient processing  Image interpretation

Two 、 flow [FILE]

  • FILE: standard IO Use a structure type to store information about the open file [ One FILE Structure represents an open file ]

  • flow (stream):FILE Also known as flow

  • In a word, understand : standard IO All operations of are around FILE To carry out

  • The flow is divided into Binary stream Text stream
    - stay Windows Next : Text stream ( Carriage returns :\r+\n) And binary streams (\n) There's a difference
    - stay Linux Next : Text stream (\n) And binary streams (\n) No difference between
    - When the program needs Migration to Windows Run time , Consider the difference between the two , With binary stream

  • The buffer type of the stream
    - Full buffer [ When opening a normal file , The default is full buffer ]: When the buffer of the stream has no data ( Write operations )、 No space ( Read operations ) when , Conduct IO operation
    - The line buffer [ Standard input and output ( When a flow is associated with a terminal ) when ]: When line breaks are encountered in input and output (\n) when , Conduct IO operation
    - No buffer [ When outputting error stream data ]: Standard error stream output time data , Write directly to file , Stream is not buffered

  • standard IO Predefined three flows :stdin【 Standard input stream 】 / stdout 【 Standard output stream 】/ stderr【 Standard error flow 】

3、 ... and 、 Opening and closing of flow

open
  • Open a standard IO flow
    **FILE *fopen(const char path, const char mode);
    Return stream pointer on success , Return... On error NULL;

    • path Is the path of the stream
    • mode Is the way to open a stream (r(b)/r+b w(b)/w+b a(b)/a+b)
#include<stdio.h>

int main(int argc, char *argv[])
{
    
	FILE *fp;
	if ((fp = fopen('demo.txt', "w+")) == NULL) {
    
		perror("fopen error");
		return -1;
	}
...
return 0;
}
- perror It's a standard. IO One way to handle error messages : Output string first s, Output the error message corresponding to the error number  
-  If there is an error in the above program, it will output :fopen error:No such file or directory
close
  • Close a standard IO flow
    *int fclose(FILE stream);
    Successfully returns 0; Failure to return EOF, And set up errno(errno Store error number )

    • Automatically flushes the data in the buffer and releases the buffer when the stream is closed
    • Once the stream is closed, no operation can be performed

Four 、 Read write flow

  • Stream supports different reading and writing modes
    1. Read and write a character :fgetc()/fputc() Once read / Write a character
    2. Read and write a line :fgets()/fputs() Once read / Write a line
Press character input output
  • Type... By character
    - int fgetc(FILE *stream);
    - Return the read characters when successful ; If it reaches the end of the file or if there is an error, it returns EOF(-1)
### Part of the code block 
FILE *fp;
int ch, count;
if ((fp = fopen(argv[1], “r”)) == NULL ) {
    
	perror(“fopen”);
	return -1;
}
while ((ch = fgetc(fp) != EOF) {
    
	count++;
}
printf(“total bytes %d \n”, count);

  • Output by character
    - int fputc(int c, FILE *stream);
    - Returns the characters written on success ; Return... On error EOF(-1)
### Part of the code block 
FILE *fp;
int ch;
if ((fp = fopen(argv[1], “w”)) == NULL) {
    
	perror(“fopen”);
	return -1;
}
for (ch = ‘a’; ch <= ‘z’; ch++) {
    
	fputc(ch, fp);
}

Input and output by line
  • Press the line to enter
    - char *fgets(char *s, int size, FILE *stream);
    - Return on success s, To the end of the file or in case of an error NULL
    - encounter ’\n’ Or entered size-1 Returns... Characters , It always ends with ’\0’
### Part of the code block 
#define N 6;
char buf[N]
fgets(buf, N, stdin);
printf(%s”, buf);
 -  Suppose the keyboard input is :abcd< enter >  abcdef< enter >,buf The content of is ?

 Insert picture description here

  • Press line to output
    - int fputs(conts char *s, FILE *stream);
    - The number of characters to be output when successful , Return... On error EOF(-1)
### Part of the code block 
FILE *fp;
char buf[] “hello world”;
if((fp = fopen(argv[1], “a”)) == NULL) {
    
	perror(“fopen”);
	return -1;
}
fputs(buf, fp);

OK 了 , Deere friends,this chapter is over, see you next

原网站

版权声明
本文为[Renax AI (Linux AI)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221448115405.html