#include <stdio.h>
|
FILE *
fopen (const char * restrict path, const char * restrict mode); |
FILE *
fdopen (int fildes, const char *mode); |
FILE *
freopen (const char *path, const char *mode, FILE *stream); |
The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.):
"r" | Open text file for reading. The stream is positioned at the beginning of the file. |
"r+" | Open for reading and writing. The stream is positioned at the beginning of the file. |
"w" | Truncate to zero length or create text file for writing. The stream is positioned at the beginning of the file. |
"w+" | Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. |
"a" | Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek or similar. |
"a+" | Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek or similar. |
The mode string can also include the letter ‘‘b’’ either as a third character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with -isoC and has no effect; the ‘‘b’’ is ignored.
Reads and writes may be intermixed on read/write streams in any order, and do not require an intermediate seek as in previous versions of stdio. This is not portable to other systems, however; ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file.
The fdopen function associates a stream with the existing file descriptor, fildes. The mode of the stream must be compatible with the mode of the file descriptor. When the stream is closed via fclose, fildes is closed also.
The freopen function opens the file whose name is the string pointed to by path and associates the stream pointed to by stream with it. The original stream (if it exists) is closed. The mode argument is used just as in the fopen function.
If the path argument is NULL, freopen attempts to re-open the file associated with stream with a new mode. The new mode must be compatible with the mode that the stream was originally opened with:
The primary use of the freopen function is to change the file associated with a standard text stream (stderr, stdin, or stdout).
/****************** this program shows opening in write mode,write data and close **************/ /****************** again open in append mode and write data***** ******************************/ /***************************** Check file c:\fopen.txt*****************************************/ #include <stdio.h> int main(void) { FILE *fp; char name[20] = "c:\fopen1.txt"; if ((fp = fopen (name, "w")) == NULL) { printf("Error creating file"); return -1; } printf("Opened file \n"); fprintf(fp, "This is the first line\n"); printf("Wrote to file\n"); fclose (fp); printf("Closed file\n"); if ((fp = fopen (name, "a")) == NULL) { printf("Error opening file"); return -1; } printf("Opened file for appending\n"); fprintf(fp, "This is the second line\n"); fclose (fp); printf("closed file, check output in c:\ fopen.txt file\n"); unlink(name); return 0; }
Output
Opened file Wrote to file Closed file Opened file for appending closed file, check output in c:\ fopen.txt file
[EINVAL] | |
The mode argument to fopen, fdopen, or freopen was invalid. | |
The fopen, fdopen and freopen functions may also fail and set errno for any of the errors specified for the routine malloc.
The fopen function may also fail and set errno for any of the errors specified for the routine open.
The fdopen function may also fail and set errno for any of the errors specified for the routine fcntl.
The freopen function may also fail and set errno for any of the errors specified for the routines
1) Mode values for group and others will be ignored for file creation, execute bit and setuid on exec bit on an file do not
have any effect while file creation.Default working
directoy of a process is initialized to C:
rivate
ID (UID of the calling application), and any data written into this C:
rivate
ID directory persists between phone resets.
2) If the specified file is a symbolic link and the file it is pointing to is invalid, the symbolic link file will be removed automatically.
© 2005-2007 Nokia |