当前位置:网站首页>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
边栏推荐
- Mono fourni avec l'unit é 5 peut également supporter C # 6
- JUnit中的@Transactional消失不见,@Rollback是否能单抗测试回滚的大旗?
- Source insight shortcut key cross reference
- The R language uses the sink function to export the string to the txt file in the specified directory. If no directory is specified, it will be output to the current working dir
- 【C】【时间操作】C语言中关于时间的操作
- Unity开发相关的博客收集
- 如何监听DOM元素尺寸的变化?
- R language uses as The character function converts date vector data to string (character) vector data
- R language factor variable type: use factor function to convert string vector to factor vector, and use as The factor function converts a factor vector into a string vector and uses as The numeric fun
- Application configuration management, basic principle analysis
猜你喜欢

How to connect the Internet - FTTH

Unity写多线程注意事项

声临其境 — 音频沉浸体验

GQL+Nodejs+MySQL数据库

Six methods of optimizing inventory management in food production industry

Binary search (integer binary)

【MGT】代码解读之model-MGT

Zhihu wanzan: what kind of programmers are still wanted by the company after the age of 35? Breaking the "middle age crisis" of programmers
![[early knowledge of activities] list of recent activities of livevideostack](/img/8c/f8007931b1a5944f3a0a243a5afcc4.png)
[early knowledge of activities] list of recent activities of livevideostack

【VS】【使用问题】【解决方案】VS2010打开一直停留在启动界面
随机推荐
[vs], [usage problem], [solution] when VS2010 is opened, it stays in the startup interface
GQL+Nodejs+MySQL数据库
Visual studio code annotation plug-in: korofileheader
基于Retrotfit2.1+Material Design+ijkplayer开发的一个APP
Six methods of optimizing inventory management in food production industry
Reading method (three times reading method)
Retrofit extended reading
4.9 commander. js
Observation on the salary data of the post-90s: poor, counselled and serious
【活动早知道】LiveVideoStack近期活动一览
Retrofit擴展閱讀
\Processing method of ufeff
Character function and string function
南京理工大学MOOC慕课:程序设计基础(Ⅰ)第8章测试选择题答案及解析
Unmanned, automation technology affects the world
Binary search (integer binary)
Retrofit Extended reading
Merge sort of sorting
Unity中.Meta文件作用详解
Post process basic notes (important items)