|
|
1.1 root 1:
2:
3: fopen() STDIO Function fopen()
4:
5:
6:
7:
8: Open a stream for standard I/O
9:
10: #include <stdio.h>
11: FFIILLEE *ffooppeenn (_n_a_m_e, _t_y_p_e) cchhaarr *_n_a_m_e, *_t_y_p_e;
12:
13: fopen allocates and initializes a FILE structure, or stream;
14: opens or creates the file name; and returns a pointer to the
15: structure for use by other STDIO routines. name refers to the
16: file to be opened. type is a string that consists of one or more
17: of the characters ``rwa'', to indicate the mode of the string, as
18: follows:
19:
20:
21: rr Read; error if file not found
22: ww Write; truncate if found, create if not found
23:
24:
25:
26: aa Append to end of file; no truncation, create if not found
27: rr+ Read and write; no truncation, error if not found
28:
29:
30:
31: ww+ Write and read; truncate if found, create if not found
32: aa+ Append and read; no truncation, create if not found
33:
34:
35: The modes that contain `a' set the seek pointer to point at the
36: end of the file; all other modes set it to point at the beginning
37: of the file. Modes that contain `+' both read and write;
38: however, a program must call ffsseeeekk or rreewwiinndd before it switches
39: from reading to writing or vice versa.
40:
41: ***** Example ***** This example copies argv[1] to argv[2] using
42: STDIO routines. It demonstrates the functions fopen, fread,
43: fwrite, fclose, and feof.
44:
45:
46: #include <stdio.h>
47: /* BUFSIZ is defined in stdio.h */
48: char buf[BUFSIZ];
49:
50:
51:
52: void fatal(message)
53: char *message;
54: {
55: fprintf(stderr, "copy: %s\n", message);
56: exit(1);
57: }
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: fopen() STDIO Function fopen()
70:
71:
72:
73: main(argc, argv)
74: int argc; char *argv[];
75: {
76: register FILE *ifp, *ofp;
77: register unsigned int n;
78:
79:
80:
81: if (argc != 3)
82: fatal("Usage: copy source destination");
83: if ((ifp = fopen(argv[1], "r")) == NULL)
84: fatal("cannot open input file");
85: if ((ofp = fopen(argv[2], "w")) == NULL)
86: fatal("cannot open output file");
87:
88:
89:
90: while ((n = fread(buf, 1, BUFSIZ, ifp)) != 0) {
91: if (fwrite(buf, 1, n, ofp) != n)
92: fatal("write error");
93: }
94:
95:
96:
97: if (!feof(ifp))
98: fatal("read error");
99: if (fclose(ifp) == EOF || fclose(ofp) == EOF)
100: fatal("cannot close");
101: exit(0);
102: }
103:
104:
105: ***** See Also *****
106:
107: fclose(), fdopen(), freopen(), STDIO
108:
109: ***** Diagnostics *****
110:
111: fopen returns NULL if it cannot allocate a FILE structure, if the
112: type string is nonsense, or if the call to open or creat fails.
113: Currently, only 20 FILE structures can be allocated per program,
114: including stdin, stdout, and stderr.
115:
116: ***** Notes *****
117:
118: Many operating systems recognize a `b' modifier to the type argu-
119: ment; this indicates that the file contains binary information,
120: and lets the operating system handle ``funny characters'' cor-
121: rectly. COHERENT has no need of such a modifier, so if you ap-
122: pend `b' to type, it will be ignored. This modifier, however, is
123: recognized by numerous other operating systems, including MS-DOS,
124: OS/2, and GEMDOS. If you expect to port developed code to any of
125: these operating systems, files should append the `b' to type.
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.