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

 Insert picture description here

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

原网站

版权声明
本文为[wekidi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170510142088.html