c/c++ 两种文件流用法
生活随笔
收集整理的這篇文章主要介紹了
c/c++ 两种文件流用法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
以刪除空白行為例?
c中,一個(gè)字符一個(gè)字符串地讀有點(diǎn)慢
#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; }?
總結(jié)
以上是生活随笔為你收集整理的c/c++ 两种文件流用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: uva1347Tour
- 下一篇: c++ 随机字符串_关于Python的随