#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
void Trim(string &str){ // 去掉字符串首尾的特殊字符 \f:换页 \v:垂直制表 \r:回车 \t:水平制表 \n:换行
string blanks("\f\v\r\t\n");
str.erase(0, str.find_first_not_of(blanks));
str.erase(str.find_last_not_of(blanks)+1);
//去掉空格
str.erase(0, str.find_first_not_of(" "));
str.erase(str.find_last_not_of(" ")+1);
}
int main(int argc, const char * argv[]) {
// insert code here...
/*
filebuf fb;
string filename = "/Users/lizhong/Documents/test/in.txt";
if(fb.open(filename.c_str(), ios::in) == NULL){
perror("open faild_1");
exit(EXIT_FAILURE);
}
istream is(&fb);
ofstream os;
os.open("/Users/lizhong/Documents/test/out.txt", ofstream::out | ofstream::app);
if(os.fail()){
perror("open faild_2");
exit(EXIT_FAILURE); //EXIT_FAILURE = 0
}
string input;
while(getline(is, input,'\n')){
auto pos = string::npos;
pos = input.find(".");
if(pos!=string::npos){
os << input.substr(pos+1) << endl;
}
}
fb.close();
os.close();
*/
ifstream is;
is.open("/Users/lizhong/Documents/test/out.txt");
if(is.fail()){
perror("open fail_1");
exit(EXIT_FAILURE);
}
string line;
vector<string> str;
while(getline(is, line, '\n')){
Trim(line);
if(isalpha(line[0])){
line[0] = toupper(line[0]);
}
str.push_back(line);
}
sort(str.begin(), str.end(), [](string a, string b){ return a>b; });
ofstream os;
os.open("/Users/lizhong/Documents/test/out1.txt", ofstream::out | ofstream::app);
if(os.fail()){
perror("open_fail_2");
exit(EXIT_FAILURE);
}
string s1;
while(str.empty()!=true){
s1 = str.back();
str.pop_back();
os << s1 << endl;
}
is.close();
os.close();
std::cout << "Hello, World!\n";
return 0;
}
|