读书人

新手求教-读取文件换行符号有关问题

发布时间: 2012-06-20 20:37:21 作者: rapoo

新手求教--读取文件换行符号问题
想记录文件有多少行记录,因此用ch1=='\n',但最后n=0,后来换成'\r'也是不行。请各位大侠指教!谢谢!
[edw@edw_ora] $ cat file_write.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
FILE *fp,*fp1;
char ch, ch1;
int n=0;
if((fp=fopen("SQL_DATA.txt","rt+"))==NULL)
{
printf("\nCannot open file, Pleas strike any key exit!");
getchar();
exit(1);
}
printf("Input source file:\n");
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
printf("\n");

fp=fopen("SQL_DATA.txt","at+");

if((fp1=fopen("file_exe.c","rt"))==NULL)
{
printf("\nCannot open file, Pleas strike any key exit!");
getchar();
exit(1);
}
/*
ch1=fgetc(fp1);
while(ch1!=EOF)
{
putchar(ch1);
ch1=fgetc(fp1);
}
*/
ch1=fgetc(fp1);
while(ch1!=EOF)
{
//fprintf(fp,"%c",ch1);
fputc(ch1,fp);
if(ch1=='\n')
{
n+=1;
}
ch1=fgetc(fp1);
}
fclose(fp1);
fclose(fp);
printf("new add record:%d\n",n);

//rewind(fp);
fp=fopen("SQL_DATA.txt","rt");
printf("Input new file:\n");
n=0;
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
if(ch1=='\n')
{
n+=1;
}
ch=fgetc(fp);
}

fclose(fp);
printf("File Total Records:%d\n",n);
printf("\n");
}

[edw@edw_ora] $
[edw@edw_ora] $ cat SQL_DATA.txt
ri Aug 6 13:03:00 1971
Fri Aug 6 13:03:00 1971
Fri Aug 6 13:03:00 1971
Mon Jun 11 02:38:01 2012
[edw@edw_ora] $ cat file_exe.c
#include <stdio.h>
#include <time.h>

int main()
{
FILE *fp;
time_t timep;
fp = fopen("SQL_DATA_2.txt","r");
time(&timep);
fprintf(fp,"%s",asctime(gmtime(&timep)));
fclose(fp);

}

[edw@edw_ora] $ gcc -std=c99 file_write.c -o file_write
[edw@edw_ora] $ file_write
Input source file:
ri Aug 6 13:03:00 1971
Fri Aug 6 13:03:00 1971
Fri Aug 6 13:03:00 1971
Mon Jun 11 02:38:01 2012

new add record:0
Input new file:
ri Aug 6 13:03:00 1971
Fri Aug 6 13:03:00 1971
Fri Aug 6 13:03:00 1971
Mon Jun 11 02:38:01 2012
#include <stdio.h>
#include <time.h>

int main()
{
FILE *fp;
time_t timep;
fp = fopen("SQL_DATA_2.txt","r");
time(&timep);
fprintf(fp,"%s",asctime(gmtime(&timep)));


fclose(fp);

}

File Total Records:0

[edw@edw_ora] $
[edw@edw_ora] $

[解决办法]
仅供参考

C/C++ code
#include <stdio.h>#include <string.h>#define MAXLEN 1000char 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;}
[解决办法]
恩 使用计数器

读书人网 >C语言

热点推荐