谁知道如何给自己的程序段落前加上行数:
比如:1
2
这样方便到哪个行里面找到错误:
我每次都是自己数行数,很累!
请大家帮忙!
对fstream不太熟悉,只能写成这样了,希望了解的指点下。。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct line{
string str;
line * next;
};
int main()
{
ifstream fin("input.cpp");
string str0;
int i=1;
line *p,*head,*q;
char step[4];
while(getline(fin,str0,'\n'))
{
itoa(i,step,10);
str0 = "*/" + str0;
str0 = step + str0;
str0 = "/*" + str0;
p = new line;
if (1 == i) head = p;
p->str = str0;
p->next = NULL;
if (1 != i) q->next = p;
q = p;
i++;
}
fin.close();
ofstream fout("input.cpp");
p = head;
while (NULL != p)
{
fout<<p->str<<endl;
p = p->next;
}
return 0;
}