Common File Processing Methods and link and unlink explanations

Source: Internet
Author: User

[Cpp]
/*
* Function Introduction: an access function is used to determine whether a specified file exists and whether it can be accessed in the specified mode.
* Header file: unistd. h
* The mode parameter can be one of the following:
* 00 exists only
* 02 write permission
* 04 read permission
* 06 read and write permissions
* Return value: if the file has a given mode, 0 is returned. If an error occurs,-1 is returned.
 
* Function Introduction: unlink () will delete the file specified by the pathname parameter. The folder cannot be processed. 0 is returned. Otherwise, 1 is returned. Unlink () deletes the file specified by the pathname parameter. If the file name is the last connection point, but other processes open the file, it will be deleted after all the file descriptions about the file are closed. If the pathname parameter is a symbolic connection, the connection is deleted.
* Header file: unistd. h
*/
 
# Include <iostream>
# Include <unistd. h>
# Include <stdio. h>
# Include <string>
 
Using namespace std;
 
Int main ()
{
Char strPath [50] = "f: \ test.txt ";
 
FILE * fp = fopen (strPath, "rw ");
Int status = access ("f: \ test.txt", 0); // get the file status
If (status = 0) // determines whether the file exists
{
Cout <"File exists \ n" <endl;
Fclose (fp );
If (! Unlink ("f: \ test.txt") // deletes the specified file
{
Cout <"successfully deleted files" <endl;
}
} Else
{
Cout <"No file" <endl;
Return 0;
}
}
 

[Cpp] view plaincopy
/*
* Function Introduction: the link function creates a hard link. The so-called hard link is the alias of the file. The soft link is equivalent to the file shortcut, which stores the path of the source file.
* Header file: unistd. h
*/
 
# Include <iostream>
# Include <unistd. h>
# Include <stdio. h>
# Include <string>
 
Using namespace std;
 
Int main ()
{
Char strPath [50] = "./leeboy.txt ";
Char chBackupFile [50] = "./leeboy_wang.txt"; // map the file name
 
FILE * fp = fopen (strPath, "rw ");
Int status = access ("./leeboy.txt", 0); // get the file status
If (status = 0) // determines whether the file exists
{
Cout <"File leeboy exists \ n" <endl;
Fclose (fp );
If (link (strPath, chBackupFile) =-1) // create a file hard link, which is equivalent to generating a new file, but stores the same location in the memory.
{
Cout <"File Link generation error! ";
Return 0;
}
If (unlink ("./leeboy.txt") =-1) // Delete the source file, but the backup file still exists.
{
Cout <"failed to delete the source" <endl;
}
} Else
{
Cout <"No file" <endl;
Return 0;
}
}

 

 

 

3. Get the file size

[Cpp]
/*
* Function Introduction: ftell obtains the file pointer location and obtains the file size.
* Header file: stdio. h
*/
 
# Include <iostream>
# Include <stdio. h>
 
Using namespace std;
 
Int main ()
{
FILE * fp = fopen ("f: \ lee.txt", "rw"); // open the FILE
Fseek (fp, 0, SEEK_END); // specifies the end of the object.
Int len = ftell (fp); // get the pointer location to get the file size
Cout <"file size:" <len <endl;
}

Author: Leeboy_Wang


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.