Send the Buddha to the west, the good man to the end
Using the C + + standard library to implement
Introduction to/*c language standard I/O function
1. library files
Typically located in the <stdio.h> or <fcntl.h> library
2. Function Introduction
2.1 FILE * fopen (const char * filepath,const char * type);
Open File
There are mainly modes of
b-> by Word throttling
W-> clears the original file contents or creates the file
A-> Append files
R-> Read files
These functions can be combined using the
Successful return is not NULL, failure is empty,
2.2. int fclose ()
Close File
Successfully returned to 0, failed to EOF
2.3 int fread (void * buff,size_t size,size_t nobj,file * fp);
int fwrite (const void * buff,size_t size,size_t nobj,file * fp);
File read and Write functions
Buff is a buffer for storing content
The size is the sizeof () of the structure.
Nobj is the number of times that a read or write
FP is a file pointer
Fread read files, ret < nobj in two cases, the end of the file or the error occurred
Fwrite when writing a file, ret < nobj is an error
2.4 int feof (FILE *fp);
File end detection function, only after the read operation, feof bit, default is false.
If the file ends, the return value is not 0, otherwise 0
2.5 int ferror (FILE *fp)
Read-write File error detection function
A return value of 0 indicates no error or errors
2.6 void Clearerr (FILE *fp)
File error flag and file end flag 0 function function
Used to clear error flags and file end flags so that they are 0 values
3. Demo Code
The following code by copying a file, a good demonstration of the above function calls, please read.
void FileCopy ()
{
std::string srcfile = "C://test.rar";
std::string destfile = "C://bbb.rar";
FILE *fpsrc = fopen (Srcfile.c_str (), "r+b");
if (fpsrc = NULL)
{
Failed to open file
Return
}
FILE *fpdest = fopen (Destfile.c_str (), "w+b");
if (fpdest = NULL)
{
Failed to create file or purge file contents
Fclose (FPSRC);
Return
}
while (feof (fpsrc) = = 0)
{
Char buff[1024 * 64];
memset (buff,0,sizeof (buff));
int readlen = Fread (buff,sizeof (char), sizeof (buff), fpsrc);
if (Readlen < sizeof (buff))
{
if (Ferror (FPSRC))
{
An error occurred in the Read
Fclose (FPSRC);
Fclose (fpdest);
}
}
if (Fwrite (buff,sizeof (char), readlen,fpdest) < Readlen)
{
Write error
Fclose (FPSRC);
Fclose (fpdest);
}
}
Fclose (FPSRC);
Fclose (fpdest);
}