fprintf

fopen

#include <stdio.h>FILE *fopen(const char *fname, const char *mode);

The fopen( ) function opens a file whose name is pointed to by fname and returns the stream that is associated with it. The types of operations that will be allowed on the file are defined by the value of mode. The legal values for mode are shown in the following table. The filename must be a string of characters comprising a valid filename as defined by the operating system and may include a path specification if the environment supports it.

In C99, fname and mode are qualified by restrict.

Mode

Meaning

"r"

Open text file for reading

"w"

Create a text file for writing

"a"

Append to text file

"rb"

Open binary file for reading

"wb"

Create binary file for writing

"ab"

Append to a binary file

"r+"

Open text file for read/write

"w+"

Create text file for read/write

"a+"

Open text file for read/write

"rb+" or "r+b"

Open binary file for read/write

"wb+" or "w+b"

Create binary file for read/write

"ab+" or "a+b"

Open binary file for read/write

If fopen( ) is successful in opening the specified file, a FILE pointer is returned. If the file cannot be opened, a null pointer is returned.

Programming Tip 

Any file can be opened as either a text file or a binary file. It does not matter what the file actually contains. For example, a file that holds ASCII text can still be opened and operated upon as a binary file. As far as the standard C file system is concerned, the only difference between a text file and a binary file is that no character translations will take place when operating on a file opened in binary mode.

You might want to open a file that contains text as a binary file when you are performing various non-text-based manipulations on it. For example, file utilities that compare, compress, or sort will typically open the files for binary access. Also, file encryption programs will almost always need to operate in binary mode.

The key point is that the difference between a text file and a binary file is not what the file contains, but rather the mode in which you open it.

As the table shows, a file can be opened in either text or binary mode. In text mode, some character translations may occur. For example, newlines may be converted into carriage return/linefeed sequences. No such translations occur on binary files.

The correct method of opening a file is illustrated by this code fragment:

FILE *fp; if ((fp = fopen("test", "w"))==NULL) {   printf("Cannot open file.\n");   exit(1); }

This method detects any error in opening a file, such as a write-protected or a full disk, before attempting to write to it.

If you use fopen( ) to open a file for output, any preexisting file by that name will be erased and a new file started. If no file by that name exists, one will be created. Opening a file for read operations requires that the file exists. If it does not exist, an error will be returned. If you want to add to the end of the file, you must use mode "a". If the file does not exist, it will be created.

When accessing a file opened for read/write operations, you cannot follow an output operation with an input operation without first calling fflush( ), fseek( ), fsetpos( ), or rewind( ). Also, you cannot follow an input operation with an output operation without first calling one of the previously mentioned functions, except when the end of the file is reached during input. That is, output can directly follow input at the end of the file.

A minimum of FOPEN_MAX files can be open at any one time. FOPEN_MAX is defined in <stdio.h>.

Related functions are fclose( ), fread( ), fwrite( ), putc( ), and getc( ).




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net