document

file buffer
The file buffer is a certain space reserved in the memory area for temporarily storing file data during reading and writing. Using a file buffer reduces the number of hard disk reads.
The getcar () function performs character input, not directly reading the input characters from the keyboard hardware, but the characters obtained from the "input buffer".
Features of the software: data can be stored for a long time
Existence form of the file 1) Character form (ASCII) such as text form 2) Binary form such as photo word
define file pointer
open file file pointer
FILE*pFile=fopen("absolute path","open method"); 
open way
FILE* pFile = NULL;
fopen_s(&pFile,"G:\\a.txt","r+");
Note that later versions of VS2002 cannot be opened with fopen, which will be considered unsafe. Use fopen_s(FILE**, "path", "open method") The path is an absolute path and cannot be copied directly from the properties. The '\' in the wrong path is easy to be used as an escape character, so when encountering '\', add a \
read file 
size_t fread (where to put it, how big, how many (a few blocks, several times), read from that file)//Generally, the definition of the size is 1 
  FILE* pFile = NULL;
  fopen_s(&pFile,"G:\\a.txt","r+");
 char str[10] = {0};
 char str1[10] = {0};
 int nRead = fread(str,1,5,pFile);
 fread(str1,1,3,pFile);
 printf("%s\n",str);
 printf("%s\n",str1);

The return value of the fread function is a few blocks. When reading the file, the pointer of the function is moved. The second time it is read, it is connected to the previous one.
write file
size_t fread (from that address, how big, how many, write to that file)
FILE* pFile = NULL;
fopen_s(&pFile,"G:\\a.txt","r+");
fwrite("abc",1,3,pFile);
//When writing a file, pay attention to the opening method of the file, which is the permission 
When it is r+, it is to directly write a few overwriting the beginning of the file
For w/w+ it will delete and rewrite the original file 
Also note that you can read continuously, you can write continuously, but you can't finish reading and writing and finish reading 

Purpose of fseek: Control file pointer offset.

Function prototype: int fseek(FILE *stream, long offset, int fromwhere)

Parameter 1 is the file stream pointer, parameter 2 is the offset size, and parameter 3 is the offset mode, usually 1: SEEK_CUR (the current position of the file) SEEK_SET (the beginning of the file) SEEK_END (the end of the file)
The fseek() function can realize the continuous problem of reading and writing  
fseek(pFile,0,SEEK_CUR); 
Don't forget to close the file when you are done with it 
fclose(pFile);

Problems with copying character form files

#include <stdio.h>

int main()
{
 FILE* pFile = NULL;
 FILE* pf = NULL;
 char c;
 fopen_s(&pFile,"g:\\1.txt","r+");//a read
 fopen_s(&pf,"g: \\2.txt","w+");//A write
 
 while((c = getc(pFile)) != EOF)//EOF means the end of the file
 {
  putc(c,pf);
 }
 fclose(pFile);
 fclose(pf);
 return 0;
}
Function description putc() will convert the parameter c to unsigned char and write it to the file specified by the parameter stream. Although putc() has the same function as fputc(), putc() is a macro definition, not a real function call.
Return value putc() will return the successfully written character, that is, the parameter c. If EOF is returned, it means the write failed 
Function description getc() is used to read a character from the file pointed to by the parameter stream. Returns EOF if the end of the file is read and there is no data. Although getc() has the same function as fgetc(), getc() is a macro definition, not a real function call.  
Return value getc() will return the characters read, and if it returns EOF, it means the end of the file.
 

copy of binary

#include <stdio.h>

int main()
{
 FILE* pf1 = NULL;
 FILE* pf2 = NULL;
 char buf[1024];
 int nRead;
 fopen_s(&pf1,"g:\\first subsection.chm","rb");//Note the permissions here plus r
 fopen_s(&pf2,"g:\\1.chm","wb");
 while(1)
 {
  nRead = fread(buf,1,1024,pf1);//This is why the return value of such a function is read one at a time and becomes how many were read
  if(nRead <= 0)
  {
   break;
  }
  fwrite(buf,1,nRead,pf2);
 }
 fclose(pf1);
 fclose(pf2);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325377994&siteId=291194637