forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileio.cpp
48 lines (43 loc) · 1.55 KB
/
fileio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// This program shows how C-based file I/O works. It will print a
// file to the screen two times.
// included so we can use cout
#include <iostream>
// stdlib.h is where exit() lives
#include <stdlib.h>
using namespace std;
// include the standard I/O library
#include <stdio.h>
// we want to use parameters
int main (int argc, char **argv) {
// verify the correct number of parameters
if ( argc != 2 ) {
cout << "Must supply the input file name as the one and only parameter" << endl;
exit(1);
}
// attempt to open the supplied file. FILE is a type designed to
// hold file pointers. The first parameter to fopen() is the
// filename. The second parameter is the mode -- "r" means it
// will read from the file.
FILE *fp = fopen(argv[1], "r");
// if the file wasn't found, output and error message and exit
if ( fp == NULL ) {
cout << "File '" << argv[1] << "' does not exist!" << endl;
exit(2);
}
// read in each character, one by one. Note that the fgetc() will
// read in a single character from a file, and returns EOF when it
// reaches the end of a file.
char g;
while ( (g = fgetc(fp)) != EOF )
cout << g;
// a nice pretty separator
cout << "----------------------------------------" << endl;
// rewinds the file pointer, so that it starts reading the file
// again from the beginning
rewind(fp);
// read the file again, and print to the screen
while ( (g = fgetc(fp)) != EOF )
cout << g;
// close the file
fclose(fp);
}