当前位置:网站首页>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

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 streamThe 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 bufferedstandard 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 :
- Read and write a character :fgetc()/fputc() Once read / Write a character
- 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 ?

- 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
边栏推荐
- Retrofit扩展阅读
- C # implement callback
- Windows10 LAN shared folder process
- Unmanned, automation technology affects the world
- The way of filling holes in APK Decompilation
- Markdown basic syntax
- How to connect the Internet - FTTH
- Is it safe to open a stock account at present? Can I open an account online directly?
- The spring recruitment is also terrible. Ali asked at the beginning of the interview: how to design a high concurrency system? I just split
- leetcode:19. Delete the penultimate node of the linked list
猜你喜欢

The difference between tuples and lists

Visual studio code annotation plug-in: korofileheader

Six methods of optimizing inventory management in food production industry

Job hopping is better than promotion

ADO. Net - invalid size for size property, 0 - ado NET - The Size property has an invalid size of 0

Qsort sort string

Abstractqueuedsynchronizer (AQS) source code detailed analysis - condition queue process analysis

Full stack development

It is said that this year gold three silver four has become gold one silver two.

Client construction and Optimization Practice
随机推荐
Retrofit擴展閱讀
How to use ADB shell to query process traffic
R language uses as The character function converts date vector data to string (character) vector data
A command starts the monitoring journey!
R language uses the < - operator to create new variables, uses the attach function to bind data, and directly uses the two data column names to calculate mean value to make new feelings in D
Unity 5 自带的Mono也可以支持C# 6
Quick sort_ sort
Post process basic notes (important items)
Requirements for setting up points sign in tasks and common problems in the process of building points mall
Client construction and Optimization Practice
Junit5 unit test
The internal structure of MySQL and how an SQL statement is executed
ADO. Net - invalid size for size property, 0 - ado NET - The Size property has an invalid size of 0
Shortcut keys accumulated when using various software
How to generate QR code
QRcode dependency
Abstractqueuedsynchronizer (AQS) source code detailed analysis - condition queue process analysis
4.8 inquirer-autocomplete-prompt
The way of filling holes in APK Decompilation
4.7 Inquirer. JS usage example