Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <string>
- #include <iostream>
- using std::string;
- using std::endl;
- using std::cout;
- using std::cerr;
- int main(int argc, char const *argv[])
- {
- if (argc < 4) {
- cerr << "Error, wrong format. Try prog.exe read/write filename \"data\"" << endl;
- return -1;
- }
- if (string(argv[1]) == "read") {
- std::ifstream inFile;
- inFile.open(string(argv[2]));
- if (!inFile.is_open()) {
- cerr << "Error: Cannot open " << argv[2] << endl;
- return -1;
- }
- string line;
- while(std::getline(inFile, line))
- cout << line << endl;
- inFile.close();
- }
- else if (string(argv[1]) == "write") {
- std::ofstream outFile(string(argv[2]), std::ios::app);
- if (!outFile.is_open()) {
- cerr << "Error: Cannot open " << argv[2] << endl;
- return -1;
- }
- outFile << argv[3] << endl;
- outFile.close();
- }
- else
- cerr << "Wrong format, try read/write" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement