The use of the fopen () function

Source: Internet
Author: User

function function: open a file

function Prototypes: FILE * fopen (const char * path,const char * mode);

correlation function:open,fclose,fopen_s,_wfopen

fopen Required Library:<stdio.h>

return value: The file pointer to the stream will be returned when the file is opened successfully. Returns null if the file fails to open, and the error code exists in errno.

In general, after opening the file will make some file read or write action, if the file fails to open, the next read and write actions can not be smooth, so generally after fopen () for error judgment and processing.

Parameter description:

The parameter path string contains the file path and filename to open, and the parameter mode string represents the flow pattern.

Mode has the following pattern strings:

R opens the file as read-only, and the file must exist.

r+ Open the file as read-write, the file must be present.

rb+ Read and write open a binary file that allows you to read and write data.

rw+ Read and write open a text file that allows reading and writing.

W Open Write-only file, if the file exists then the file length is clear to 0, that is, the contents of the file will disappear. If the file does not exist, the file is created.

w+ Open a read-write file, if the file exists then the file length is clear to zero, that is, the contents of the file will disappear. If the file does not exist, the file is created.

A write-only file opens in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. (EOF character reserved)

A + opens readable and writable files in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. (the original EOF character is not preserved)

WB only writes Open or create a new binary file; Only write data is allowed.

wb+ read-write open or create a binary file that allows reading and writing.

ab+ Read and write open a binary file that allows you to read or append data at the end of the file.

at+ open a file called string, a means append, that is, when the writing process is followed by the original file has been written, not written from scratch, t means that the type of open file is a text file, + number indicates that the file can be read or write.

These morphological strings can be added with a B character, such as RB, W+b, or ab+, and a B character is used to tell the library to open the file in binary mode. If you do not add B, the default is to add T, which is RT,WT, where T indicates that the file is opened in text mode. A 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 features, and some C versions do not use "r+", "w+", "A +", but with "RW", "WR", "AR" and so on, readers pay attention to the rules of the system used.

The difference between binary and text patterns

1. In the Windows system, in text mode, the file represents a newline with "". If you open a file in text mode and write a newline character "\ n" with a function such as fputs, the function automatically adds "\ r" before "\ n". That is, "" is actually written to the file.

2. In the text mode of the class Unix/linux system, the file represents a newline with "\ n". Therefore, there is no difference between the text mode and the binary mode in the Linux system.

Example

program Example 1

#include <stdio.h>

#include//In order to use Exit ()

int main ()

{

Char ch;

file* FP;

Char fname[50]; Used to store file names

printf ("Input file name:");

scanf ("%s", fname);

Fp=fopen (fname, "R"); For reading only

if (fp==null)//if it fails

{

printf ("Error! ");

Exit (1); Abort Program

}

GETC () to get a character in an open file

while ((CH=GETC (FP))!=eof)

Putchar (CH);

Fclose (FP); Close File

return 0;

}

Attention! Beginners often make a mistake, that is, when you enter the file name without the suffix name, please pay attention to add!

program Example 2

#include <stdio.h>

FILE *stream, *stream2;

int main (void)

{

int numclosed;

fopen//Open for read (would fail if file "CRT_FOPEN.C" does not exist)

if (stream = fopen ("Crt_fopen.c", "r")) = = = NULL)//C4996

Note:fopenis deprecated; Consider Usingfopen_sinstead

printf ("The file ' crt_fopen.c ' was notopened\n");

Else

printf ("The file ' crt_fopen.c ' wasopened\n");

Open for Write

if (stream2 = fopen ("Data2", "w+") = = = NULL)//C4996

printf ("The file ' data2 ' is not opened\n");

Else

printf ("The file ' data2 ' was opened\n");

Close Stream If it is not NULL

if (stream)

{

if (fclose (stream))

{

printf ("The file ' crt_fopen.c ' is not closed\n");

}

}

All other files is closed:

numclosed = _fcloseall ();

printf ("Number of files closed by _fcloseall:%u\n", numclosed);

}

<:SECTION>

2 Operation

[1] In the file operation, the following points need to be aware of

1, when defining the file pointer, you want to point the file pointer to empty, such as file *FP = NULL;

2, the file operation is completed, you need to close the file, it is important to note that this will cause the file to occupy the memory leak and the next time the file access problems.

3. When the file is closed, the file pointer needs to be pointed to null, which will prevent the free pointer from appearing, and cause unnecessary trouble to the whole project; fp = NULL;

Sample program:

#include <stdio.h>

#define F_path "D:\myfile\file.dat"

int main (void)

{

FILE *FP = NULL; Need to be aware

fp = fopen (F_path, "R");

if (NULL = = fp)

{

return-1; To return an error code

}

Fclose (FP);

fp = NULL; Need to point to null, otherwise it will point to the original open file address

return 0;

}

The use of the fopen () function

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.