# Include "stdio. h"
# Include "malloc. h"
# Include <sys/stat. h>
// Get the file size
Long GetSize (char * path)
{
// Obtain the file size in the first method
Long currentPosition;
Long size;
FILE * fp;
Fp = fopen (path, "rb ");
/* Save the current position .*/
CurrentPosition = ftell (fp );
/* Jump to the end of the file .*/
Fseek (fp, 0L, SEEK_END );
/* Get the end position .*/
Size = ftell (fp );
/* Jump back to the original position .*/
Fseek (fp, currentPosition, SEEK_SET );
Return size;
// The second method obtains the file size, which must reference sys/stat. h.
/* Struct stat buf;
If (stat (path, & buf )! =-1 ){
Return buf. st_size;
}
Return-1 ;*/
Fclose (fp );
}
Void ReadTxt (char * path)
{
FILE * file = fopen (path, "rt ");
If (file = NULL)
{
Printf ("the file does not exist \ n ");
Return;
}
// The following two methods are essentially allocating a 10-byte storage unit, and then declaring a character pointer pointing to the memory address of the first byte. After opening the file in the form of a read text file, 9 characters are read from the array and sent to the str array. '\ 0' is added to the last unit of the array, and the output str array is displayed on the screen.
// Or char str [10];
Char * str = (char *) malloc (10 );
Fgets (str, 10, file );
// Only 9 characters are output
Printf (str );
Printf ("\ n ");
Fclose (file );
Free (str );
Str = NULL;
}
// Read characters
Void ReadChar (char * path)
{
FILE * file = fopen (path, "rt ");
Char c;
If (file = NULL)
{
Printf ("the file does not exist \ n ");
Return;
}
// Fgetc is the same as getc, and fputc is the same as putc.
While (c = getc (file ))! = EOF)
{
Printf ("% c", c );
}
Printf ("\ n ");
Fclose (file );
}
Void ReadTxtByLength (char * path)
{
// Because the fgets method adds '\ 0' to the end of the character array, GetSize (path) + 1
Long length = GetSize (path) + 1;
FILE * file = fopen (path, "rt ");
If (file = NULL)
{
Printf ("the file does not exist \ n ");
Return;
}
// The following two methods are essentially allocating a 10-byte storage unit, and then declaring a character pointer pointing to the memory address of the first byte. After opening the file in the form of a read text file, 9 characters are read from the array and sent to the str array. '\ 0' is added to the last unit of the array, and the output str array is displayed on the screen.
// Or char str [10];
Char * str = (char *) malloc (length );
Fgets (str, length, file );
// Only 9 characters are output
Printf (str );
Printf ("\ n ");
Fclose (file );
Free (str );
Str = NULL;
}
# Define type void
// Copy an object
Void CopyFile (char * originalPath, char * desPath)
{
Long length = GetSize (originalPath );
// Any pointer type can be used here, such as int, char, FILE, void, etc.
Type * buffer = (type *) malloc (length );
FILE * fp = fopen (originalPath, "rb ");
Fread (buffer, length, 1, fp );
Fclose (fp );
FILE * newFile = fopen (desPath, "wb ");
Fwrite (buffer, length, 1, newFile );
Fflush (newFile );
Fclose (newFile );
}
Void main ()
{
// ReadChar ("D: \ 1.txt ");
// ReadTxt ("D: \ 1.txt ");
// ReadTxtByLength ("D: \ c.txt ");
CopyFile ("D :\\ a.rar", "D :\\ B .rar ");
}