C language operation file Summary

Source: Internet
Author: User
# 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 ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.