当前位置:网站首页>C language | file operation and error prone points
C language | file operation and error prone points
2022-06-26 14:09:00 【wekidi】
File operations
Files are stored in the operating system in a page block size , No matter how many bytes The beginning is 4k, super 4k Turn into 8k
file Is a file operation structure , When you need to operate on a file , You need to define a file operation pointer
Open file
Open file function prototype :FILE* fopen ( const char * filename, const char * mode );
filename: route , If there is no folder, an error will be reported
mode: How to read and write
int main()
{
FILE* fp=fopen("d:\\fxl.txt","w");
}
Read and write files
fprinf(fp,"%d ",ar[i]);//ASCII Code output to fp in itoa function
fscanf(fp,"%d",&br[i];// Write to br in be used itoa function , Scan to non numeric characters and stop
// When used , When it's divided
fscanf(fp,"%d,",&br[i])// We should add , When writing, the , As part of the format , Make the next read fp Time is in numbers, not , On
atoi() The function scans the parameters str character string , Skip the preceding white space ( Such as spaces ,tab Indentation, etc. , Can pass isspace() Function to detect ), It's not until you meet a number or a sign that you start to convert , And when it comes to the end of a non number or string (’\0’) To end the conversion , And return the result .
So use , Other than white space characters ( Such as spaces ,tab Indent ) Other symbols than , The format should be written strictly according to the format
Reading and writing of binary files
:size_t fwrite ( const void * ptr, size_t size, size_t count,FILE * stream );
ptr: This is a pointer to the array of elements to be written .
size: This is the size of each element to be written , In bytes .count: This is the number of elements , The size of each element is size byte .
stream: This is pointing to FILE Object pointer , The FILE Object specifies an output stream .
What form is written in the memory int a=1; In memory is 01000000( The small end ) So when reading, you should offset according to the size of the integer
Close file
file location
long ftell ( FILE *stream ) ; Returns the current file location , Bytes are units
fpos_t fgetpos( FILE *stream,fpos_t *ps) fpos_t by 64 position int The function is the same as above
int fseek( FILE*stream, long offset, int origin ); Successful implementation 0
Set file stream stream The file location indicator for is offset Value pointed to .
1) if stream Open in binary mode , The new location is exactly after the beginning of the file ( if origin by SEEK SET) Or after the current file location ( if origin by SEEK_CUR ), Or after the end of the file ( if origin by SEEKEND) Of offset byte . Binary stream support is not required SEEK_END, In particular, whether to output additional empty bytes .
2) if stream Open in text mode , Only supported offset Value is zero ( Can be used for any origin) And previously on a stream associated with the same file ftell The return value of the call to ( Only for SEEK_SET Of origin ).
3) if stream For wide face , The restrictions on text and binary streams are applied together ( allow ftell The result of SEEK_SET Use together , And allow zero offset With SEEK_SET and SEEK_CUR But not SEEK_END Benchmarking ).
In addition to changing the file location indicator ,fseek Also revoke ungetc And clear the end of file status , If applicable . If a read or write error occurs , Set the error indicator of the stream ( ferror ) Without affecting the file location .
stream: File stream to modify
offset: relative origin Number of characters migrated
origin: offset The position added . It can have one of the following values : SEEK_SET、SEEK_CUR、SEEK_END
int feof( FILE*stream );
// adopt ftell and fseek Get the number of bytes
fseek(pf,0,SEEK_END);
int len=ftell(pf);// If opened as text , The carriage return will be counted into two bytes , When the buffer is loaded, it is merged into one , So the length obtained is longer than the actual length
// Binary does not
fgetc getc Get a character from the file stream
fgets Get a string from the file stream
fputc putc Write a character to the file stream
fputs Write a string to the file stream
getchar from stdin Read a character
gets(C11 Remove )
gets_s from stdin Read a string
putchar Write a character to stdout
puts Write a string to stdout
ungetc Send a character back to the file stream
getchar() Read a character from the buffer
char cf;
cf=getchar();
// If input 1 enter , Then the data input from the standard input device is loaded into the buffer 1+ Enter these two data , Finished reading 1 Then there will be a return ascii In the buffer
// If there is no data in the buffer, it will be input from the standard input device
Differentiation and analysis
getchar_s() Stop at carriage return scanf_s Stop at the space , Are read from the buffer
puts amount to printf("%s \n",str);
int feof( FILE*stream ); and fgetc getc fgets:
getc Reading txt The last character of the document ,fp Point to the last character , After reading the characters ,fp Back ward ,fp Point to EOF, So let's do the next loop ,feof Determine whether the end of the document has been reached : Because last time fgetc The last character of the document is read , So even fp At present, it has pointed to EOF,feof Function still returns false , So the cycle continues , In the next cycle , Will be fp Point to the EOF Output ,fp Move backward ( The end of the document has been reached , Cannot move back );
fgets Do not change when encountering the end of file indicator str
The contents of the array pointed to
边栏推荐
- CloudCompare——泊松重建
- Wechat applet SetData dynamic variable value sorting
- New specification of risc-v chip architecture
- character constants
- 7.Consul服务注册与发现
- Connection migration for DataGrid configuration
- HW蓝队溯源流程详细整理
- Global variable vs local variable
- C language ---getchar() and putchar()
- Calculate the distance between two points (2D, 3D)
猜你喜欢
Win10 home vs pro vs enterprise vs enterprise LTSC
ICML 2022 | limo: a new method for rapid generation of targeted molecules
Detailed sorting of HW blue team traceability process
Codeforces Global Round 21A~D
AGCO AI frontier promotion (6.26)
Range of types
古瑞瓦特冲刺港交所上市:创下“多个第一”,获IDG资本9亿元投资
【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
RISC-V 芯片架构新规范
Postman自动化接口测试
随机推荐
Bug STL string
Es snapshot based data backup and restore
Basic type of typescript
GEE——全球人类居住区网格数据 1975-1990-2000-2014
虫子 STL string上
GO语言-管道channel
[hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
微信小程序注册指引
Applicable and inapplicable scenarios of mongodb series
AGCO AI frontier promotion (6.26)
Half search, character array definition, character array uses D11
Range of types
[path of system analyst] Chapter 15 double disk database system (database case analysis)
Select tag - uses the default text as a placeholder prompt but is not considered a valid value
Create your own cross domain proxy server
New specification of risc-v chip architecture
Record: why is there no lightning 4 interface graphics card docking station and mobile hard disk?
character constants
Global variable vs local variable
Is expression of D