当前位置:
首页 >
c/c++ 两种文件流用法
发布时间:2024/4/11
41
豆豆
生活随笔
收集整理的这篇文章主要介绍了
c/c++ 两种文件流用法
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
以删除空白行为例
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; }
总结
以上是生活随笔为你收集整理的c/c++ 两种文件流用法的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: uva1347Tour
- 下一篇: c++ 随机字符串_关于Python的随