Source code from the C ++ programming idea
Require. h some small inline functions
1: #ifndef REQUIRE_H
2: #define REQUIRE_H
3: #include <cstdio>
4: #include <cstdlib>
5: #include <fstream>
6:
7: inline void require(bool requirement,
8: const char* msg = "Requirement failed") {
9: using namespace std;
10: if (!requirement) {
11: fputs(msg, stderr);
12: fputs("\n", stderr);
13: exit(1);
14: }
15: }
16:
17: inline void requireArgs(int argc, int args,
18: const char* msg = "Must use %d arguments") {
19: using namespace std;
20: if (argc != args + 1) {
21: fprintf(stderr, msg, args);
22: fputs("\n", stderr);
23: exit(1);
24: }
25: }
26:
27: inline void requireMinArgs(int argc, int minArgs,
28: const char* msg =
29: "Must use at least %d arguments") {
30: using namespace std;
31: if(argc < minArgs + 1) {
32: fprintf(stderr, msg, minArgs);
33: fputs("\n", stderr);
34: exit(1);
35: }
36: }
37:
38: inline void assure(std::ifstream& in,
39: const char* filename = "") {
40: using namespace std;
41: if(!in) {
42: fprintf(stderr,
43: "Could not open file %s\n", filename);
44: exit(1);
45: }
46: }
47:
48: inline void assure(std::ofstream& in,
49: const char* filename = "") {
50: using namespace std;
51: if(!in) {
52: fprintf(stderr,
53: "Could not open file %s\n", filename);
54: exit(1);
55: }
56: }
Each time ifstream and ofstream are created, the assure () function is provided to ensure that the file is opened successfully.
Get () or read sz-1 characters or encounter a file of '\ n' and then add 0 terminator at the end of BUF. Get () will leave the terminator in the file in the input stream, so you need to use get () to throw the Terminator. You can also use the ignore () function to do this. The first parameter is the number of characters to be discarded. The default value is 1. The second parameter is the character to be discarded. The default value is EOF.
The Getline () function automatically removes '\ n' from the input stream. Therefore, you can directly read the data in the input stream next time. Generally, geiline () is used ().
After both get and Getline are read, a final character '0' is added at the end of the Buf '.
1: #include "../require.h"
2: #include <fstream>
3: #include <iostream>
4: using namespace std;
5:
6: int main() {
7: const int sz = 100; // Buffer size;
8: char buf[sz];
9: {
10: ifstream in("Strfile.cpp"); // Read
11: assure(in, "Strfile.cpp"); // Verify open
12: ofstream out("Strfile.out"); // Write
13: assure(out, "Strfile.out");
14: int i = 1; // Line counter
15:
16: // A less-convenient approach for line input:
17: while(in.get(buf, sz)) { // Leaves \n in input
18: in.get(); // Throw away next character (\n)
19: cout << buf << endl; // Must add \n
20: // File output just like standard I/O:
21: out << i++ << ": " << buf << endl;
22: }
23: } // Destructors close in & out
24:
25: ifstream in("Strfile.out");
26: assure(in, "Strfile.out");
27: // More convenient line input:
28: while(in.getline(buf, sz)) { // Removes \n
29: char* cp = buf;
30: while(*cp != ‘:‘)
31: cp++;
32: cp += 2; // Past ": "
33: cout << cp << endl; // Must still add \n
34: }
35: } ///:~
Differences between fstream access files and get () and Getline ()