【发布时间】:2013-12-15 20:28:48
【问题描述】:
strfile.cpp 中的代码:
#include <fstream>
#include <iostream>
#include <assert.h>
#define SZ 100
using namespace std;
int main(){
char buf[SZ];
{
ifstream in("strfile.cpp");
assert(in);
ofstream out("strfile.out");
assert(out);
int i = 1;
while(!in.eof()){
if(in.get(buf, SZ))
int a = in.get();
else{
cout << buf << endl;
out << i++ << ": " << buf << endl;
continue;
}
cout << buf << endl;
out << i++ << ": " << buf << endl;
}
}
return 0;
}
我要操作所有文件 但在 strfile.out 中:
1: #include <fstream>
2: #include <iostream>
3: #include <assert.h>
4: ...(many empty line)
我知道 fstream.getline(char*, int) 这个函数可以管理它,但是我想知道如何做到这一点只需使用函数“fstream.get()”。
【问题讨论】:
-
“如何操作”是什么意思?你想忽略空行吗?您的代码是否在空行上失败?
-
对不起我的英语不好..我是什么当有一个空行时,用行号输出它。
-
如果你想复制一个文件并在每行或 100 个字符之后插入一个行号,以先发生者为准,它对我来说很好。您能更具体地说明您的问题吗?
-
@lanmezhe: 你想只在空行上输出行号吗?
-
@molbdnilo 我知道怎么做了,还是谢谢大家!