#include<iostream> using namespace std; #include<fstream> #include<vector> #include<string> void outputToFile_app_endl(string path, string things) { ?? ?//记得include<fstream> ?? ?ofstream ofs; ?? ?ofs.open(path, ios::out | ios::app); ?? ?if (ofs.is_open()) { ?? ??? ?ofs << things; ?? ??? ?ofs << '\n'; ?? ?} } void outputToFile_app_blank(string path, string things) { ?? ?//记得include<fstream> ?? ?ofstream ofs; ?? ?ofs.open(path, ios::out | ios::app); ?? ?if (ofs.is_open()) { ?? ??? ?ofs << things; ?? ??? ?ofs << ' '; ?? ?} } void inputToProject(string path, string& str) { ?? ?ifstream ifs; ?? ?ifs.open(path, ios::in); ?? ?if (ifs.is_open()) { ?? ??? ?char ch; ?? ??? ?ifs >> ch; ?? ??? ?if (ifs.eof()) ?? ??? ?{ ?? ??? ??? ?cout << "file is back" << endl; ?? ??? ?} ?? ??? ?else { ?? ??? ??? ?ifs.putback(ch); ?? ??? ??? ?string buf; ?? ??? ??? ?while (getline(ifs, buf)) { ?? ??? ??? ??? ?str = str + buf; ?? ??? ??? ?}
?? ??? ?} ?? ?} ?? ?else { ?? ??? ?cout << "file not found" << endl; ?? ?} } void inputToProject(string path, vector<string>& str) { ?? ?ifstream ifs; ?? ?ifs.open(path, ios::in); ?? ?if (ifs.is_open()) { ?? ??? ?char ch; ?? ??? ?ifs >> ch; ?? ??? ?if (ifs.eof()) ?? ??? ?{ ?? ??? ??? ?cout << "file is back" << endl; ?? ??? ?} ?? ??? ?else { ?? ??? ??? ?ifs.putback(ch); ?? ??? ??? ?string buf; ?? ??? ??? ?while (getline(ifs, buf)) { ?? ??? ??? ??? ?str.push_back(buf); ?? ??? ??? ?}
?? ??? ?} ?? ?} ?? ?else { ?? ??? ?cout << "file not found" << endl; ?? ?} } int main() { ?? ?outputToFile_app_blank("D:\\c++file\\Project15\\textBlank.txt", "hongzh"); ?? ?outputToFile_app_endl("D:\\c++file\\Project15\\textEndl.txt", "hongzh"); ?? ?vector<string> v; ?? ?inputToProject("D:\\c++file\\Project15\\textEndl.txt", v); ?? ?for (vector<string>::iterator it = v.begin(); it < v.end(); it++) { ?? ??? ?cout << *(it) << endl; ?? ?} ?? ?return 0; }
|