64,690
社区成员
发帖
与我相关
我的任务
分享
ifstream readfile("test.log"); //1----读入文件mail2.log
if (!readfile) //打开文件失败!
{
cout << "Fail to open file in the first 1---!" << endl;
return 1;
}
else
{
int line = 0;
cout << readfile.peek() << endl;
if (!readfile.eof())
{
while (getline(readfile, strLine))//while (getline(readfile, strLine))
{ //判断空行,1-27,修改,空行不处理
if (strLine.size() != 0)
{
line = line + 1;
//cout << "this is the line number" << line << endl;
}
}
cout << strLine << endl;
}
}
#include <stdio.h>
int main() {
FILE *f;
static char ln[10000];
int i;
if (0==fopen_s(&f,"test.log","rb")) {
while (1) {
if (NULL==fgets(ln,10000,f)) break;
i++;
printf("%08d: %s",i,ln);
}
fclose(f);
}
return 0;
}
while(getline(ifs,strget))
static char ln[10000];
但是对于预先设定的静态数组大小还是不能保证正确,后面我测试了一个行有60000多个字符的,结果就被分割为了6行,所以我增加大小[/quote]
我使用这个可以实现全部行输出了,非常感谢。 static char ln[10000];
但是对于预先设定的静态数组大小还是不能保证正确,后面我测试了一个行有60000多个字符的,结果就被分割为了6行,所以我增加大小[/quote]
参考这个:
#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char ln[MAXLEN];
FILE *f;
int i,z;
int b,n,L;
int main(int argc,char **argv) {
if (argc<2) {
printf("Usage:%s fullpathfilename.ext\nget total blank/non-blank/total linenumbers.\n",argv[0]);
return 1;
}
f=fopen(argv[1],"r");
if (NULL==f) {
printf("Can not open file [%s]!\n",argv[1]);
return 2;
}
z=0;
b=0;
n=0;
L=0;
while (1) {
if (NULL==fgets(ln,MAXLEN,f)) break;
L=strlen(ln);
if ('\n'==ln[L-1]) {
if (0==z) {
for (i=0;i<L-1;i++) {
if (!(' '==ln[i] || '\t'==ln[i])) break;
}
if (i<L-1) z=1;//当前行不是空行
}
if (0==z) b++; else n++;
z=0;
} else {
if (0==z) {
for (i=0;i<L;i++) {
if (!(' '==ln[i] || '\t'==ln[i])) break;
}
if (i<L) z=1;//当前行不是空行
}
}
}
fclose(f);
if (L>0 && '\n'!=ln[L-1]) {
if (0==z) b++; else n++;//最后一行末尾无'\n'也计算
}
printf("File:[%s] total blank/non-blank/total linenumbers is %d/%d/%d\n",argv[1],b,n,b+n);
return 0;
}
static char ln[10000];
但是对于预先设定的静态数组大小还是不能保证正确,后面我测试了一个行有60000多个字符的,结果就被分割为了6行,所以我增加大小