Closing any open files and file attributes acquisition

First, open the file

Popular, the establishment of various information files, the file pointer to the file and
opens the specified file can be used fopen () function;
the fopen function prototypes

FILE *fopen(char *filename,char *mode);

Use mode mode opens the specified file filename

  • Open success, returns a pointer to type FILE
  • Open failed, return NULL

must be a string format mode, a head and tail must be enclosed in double quotes

That file mode access method

File usage significance
“rt” Read-only open a text file, allowing only read data
“wt” Write only open or create a text file, allowing only write data
“at” Additional open a text file and write data at the end of file
“rb” Read-only open a binary file, allowing only read data
“wb” Write only open or create a binary file, allowing only write data
"from" Additional open a binary file and write data at the end of file
“rt+” Reading and writing open a text file that allows reading and writing
“wt+” Reading and writing open or create a text file that allows reading and writing
“at+” Open a text file read and write, to allow reading or additional data at the end of file
“rb+” Open to read and write a binary file that allows reading and writing
“wb+” Reading and writing open or create a binary file that allows reading and writing
"Ab +" Open reading and writing a binary file that allows reading, or additional data at the end of file

Above is from the book summary, mode a little more, I have summarized a little method

  1. B binaries

    • Text files t
    • Reading r
    • Write w
    • Append a
    • Read and write +
  2. "R" to open a file, the file must already exist, and can only be read from the file

  3. "w" open files can only be written to the file;
    if the file does not exist, specify the file name of the establishment of the file
    if the specified file already exists, the file is deleted and rebuild a new file

  4. To append information to a file that already exists, the file can only be opened with "a" mode, if the file does not exist at this time, it will create a new file

  5. The following common block to open the file

FILE *fp;
fp=fopen("e:\\code\\test.txt","r");
if(fp==NULL)
{
	printf("\n不能打开e:\\code\\test.txt file!");
	exit(1);退出程序
 } 
  

Executable file and open a text file in the same path test.txt

FILE *fp;
fp=fopen("test.txt","r");

Open Next E disk folder under the code text file test.txt

FILE *fp;
fp=fopen("e:\\code\\test.txt","rt");\\第一个表示转义字符,第二个表示根目录

The first of which represents a backslash escape character, the second backslash represents the root directory

FILE *fp;
fp=fopen("e:/code/test.txt","rt");

Above all belong to the embedded file method, the following documents will introduce interactive methods, input from the keyboard you want to open the file name and path that is

FILE *fp;
char filename[40];
gets(filename);
fp=fopen(filename,"r");

Second, close the file

Use fclose () function to specify the file is closed, and related resources FILE file pointer and returned to the occupied buffer system
fclose function prototype

int fclose (FILE *fp);

Return 0, shut success; returns a non-zero, an error occurred

Third, get the file attributes

fileno function (stdlib.h)

int fileno(FILE *fp);  返回所打开文件指针fp对应的文件描述字(handle_no)

filelength function (the io.h)

long filelength(int handle_no);  返回文件描述字(handle_no)对应的文件大小,以字节为单位

E acquisition program under the code disk folder test.txt file size

FILE *fp;
int fno,fsize;
fp=fopen("e:\\code\\test.txt","rt");
fno=fileno(fp);
fsize=filelength(fno);
fclose(fp);
Published 26 original articles · won praise 6 · views 1464

Guess you like

Origin blog.csdn.net/qiao_xi/article/details/90498675