以删除空白行为例
c中,一个字符一个字符串地读有点慢
#include<iostream>
#include<string>
using namespace std;
int main() {
string filename,outfile;
cin >> filename;
freopen(filename.c_str(), "r", stdin);
outfile = "";
outfile = filename.substr(0, filename.find(".", 0)) + "out"+ filename.substr(filename.find(".", 0));
freopen(outfile.c_str(), "w", stdout);
char t=' ';
char pre = ' ';
int i = 0;
while (scanf("%c", &t)) {
if (t == pre && t == '\n') {
continue;
}
pre = t;
printf("%c", t);
if (i == 10)break;
i++;
}
return 0;
}
c++用了文件流,注意window里面换行是/r/n
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
string filename,outfile;
ifstream file1;
ofstream file2;
cin >> filename;
outfile = "";
outfile = filename.substr(0, filename.find(".", 0)) + "out"+ filename.substr(filename.find(".", 0));
file1.open(filename, ios::in);
file2.open(outfile);
int i = 0;
char *s1 = new char[10000];
while (!file1.eof()) {
file1.getline(s1,'/r/n');
if (strcmp(s1,"")) {
file2 << s1<<"\n";
}
}
return 0;
}