C language file Operation fopen, Fclose, mkdir detailed _c language

Source: Internet
Author: User
Tags error code function prototype mkdir readable

1. Build Folder

int _mkdir (const char *path,mode_t mode);
Function Name: _mkdir
Function: Create a directory
Usage: int _mkdir (const char *dirname);
Header File Library: direct.h
Return value: Creates a directory, returns 0 if successful, otherwise returns-1

=====================================================

2. Open File fopen ()

function function: Open a file
Function prototype: FILE * fopen (const char * path,const char * mode);
Required libraries: <stdio.h>
Return value: After the file is successfully opened, the file pointer to the stream is returned. Returns null if the file fails to open, and the error code is in errno.

Generally speaking, open the file will make some files read or write the action, if the file failed to open, the next reading and writing can not be smooth, so generally in fopen () after the error judgment and processing.

if (Fin = fopen ("A.txt", "r") = = NULL)
printf ("Open file error.\n");

Parameter description:
The parameter path string contains the file path and filename you want to open, and the parameter mode string represents the flow pattern.
Mode has the following morphological strings: (commonly used in the first few, after the use of a second check, do not remember)
R opens the file as read-only and the file must exist.
W Open Write-only file, if the file exists, the file length is 0, that is, the file content will disappear. If the file does not exist, the file is created.
A opens the write-only file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained. (The EOF character is reserved)

RB opens a binary file for input.
WB write-only open or create a binary file;
AB adds data to the end of the binary file

r+ opens the file in a read-write manner, and the file must exist.
w+ open a writable file, if the file exists, the file length is zero, that is, the file content will disappear. If the file does not exist, the file is created.
A + opens the writable file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained. (the original EOF character is not retained)

rb+ read-write opens a binary file that only allows reading of data.
wb+ Read and write to open or create a binary file that allows reading and writing.
ab+ read-write opens a binary file that allows you to read or append data at the end of a file.

rt+ read-Write opens a text file that allows reading and writing.
wt+ Read and write to open or create a text file;
at+ read-Write opens a text file that allows you to read or append data at the end of the text.
The above morphological strings can be added to a B-character, such as RB, W+b or ab+, add a B-character to tell the function library to open the file as a binary file, not a plain text file.
However, in the POSIX system, including Linux ignores the character. The new file created by fopen () will have a s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth (0666) permission, this file permission will also refer to the Umask value.
Some c-compiler systems may not fully provide all of these functions, some C version without "r+", "w+", "A +", and with "RW", "WR", "AR" and so on, readers pay attention to the provisions of the system.

==========================================================

3. Close File fclose ()

You should close it after using a file to prevent it from being misused again, close means that the file pointer variable does not point to the file, that is, the file pointer variable is decoupled from the file, and can no longer read and write to the file associated with the original. Unless you open it again, make the pointer variable point to the file again.

To close a file with the Fclose function, the general form of the Fclose function call is

fclose (file pointer);

For example:

Fclose (FP);

You should get into the habit of shutting down all files before the program terminates, and you will lose data if you do not close the file. Because as mentioned earlier, write data to the file, is the first data to the buffer, the buffer is full after the official output to the file, if the data is not full buffer, and the program ended running, the buffer will be lost in the data, using the Fclose function to close the file, you can avoid this problem, It outputs the data in the buffer to the disk file before releasing the file pointer variable.

The Fclose function also brings back a value, and when the shutdown operation is performed successfully, the return value is 0, otherwise, EOF (-1) is returned. You can use the Ferror function to test.
==============================================================
Here is a program to illustrate the above function usage:

Copy the a.txt to the/test directory, and the name is A.txt.

C++

/** *
 Author:gneveek *
 data:2011-10-6 *
 descripition:clock Chen ' s homework:file operation #1
 * I will A.T XT Copy to/test directory, name also for a.txt 
/#include <stdio.h>
#include <direct.h>//Create a folder (directory) with _mkdir () 
int main ()
{
 _mkdir ("test");
 
 char c;
 FILE *fin, *fout;
 if (Fin = fopen ("A.txt", "r") = = NULL)
 printf ("I ' m so sorry,open file error.\n");
 if (Fout = fopen ("Test\\a.txt", "w") = = NULL)//W, can only be used to write to the file, if it does not exist, create a new file with the specified filename
 printf ("I ' m so Sorry,open File error.\n ");
 
 while ((c = fgetc (Fin))!= EOF)
 FPUTC (c,fout); 
 
 Fclose (Fin);
 Fclose (fout);
 return 0;
}

 

================================================================

4. Determine whether the file exists and is readable and writable

int access (const char *pathname,int mode);

Pathname: is the file name
Mode is the attribute we want to judge. You can take the following values or their combination:
R_OK files can be read
W_OK files can be written
X_OK files can be executed
F_OK file exists.

When we test the success, the function returns 0, otherwise, if a condition does not match, return-1.

Use a program instance to recognize this function:

Enter a filename (entered on the command line) to determine if the file exists, is readable, does not exist, does not exist, is unreadable, if it exists and is readable, creates a directory test in the current directory, and copies the file to test

C++

/** *
 Author:gneveek *
 data:2011-10-6 *
 descripition:clock Chen's homework:file operation #2
 * Two, enter a text Part name (entered on the command line), to determine if the file exists and is readable,
 * does not exist the hint does not exist, the unreadable hint is unreadable, if it exists and is readable,
 * In the current directory, create a directory test and copy the file to test
 * 
* Include <stdio.h>
#include <io.h>
#include <direct.h> 
#include <string.h>
 
int main ()
{
 FILE *fin, *fout;
 Char filename[256]; 
 Char pathname[256] = "test\\";  
 scanf ("%s", filename);
 if (Access (FILENAME,F_OK) = = 1)//To determine whether or not printf is present 
 ("This file does not exist!\n");
 else if (access (FILENAME,R_OK) = = 1)///If present, determine whether readable 
 printf ("Cann ' t read!\n");
 else               //Here, the description is present and readable 
 {
 char C;
 _mkdir ("test"); 
 
 strcat (pathname,filename);  
 
 Fin = fopen (filename, "R"); 
 Fout = fopen (Pathname, "w");
 
 while ((c = fgetc (Fin))!= EOF)
  FPUTC (c,fout);
 }
 return 0;
}

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.