c语言。测试一个txt文本的行数和第一行的字数,为什么指针总是指到最后一行,并计算最后一行的字数,谢谢
#include<stdio.h>#include<stdlib.h>#defineBA1000intmain(){FILE*fp;chara[BA],*p;inti=0...
#include<stdio.h>
#include<stdlib.h>
#define BA 1000
int main()
{
FILE *fp;
char a[BA],*p;
int i=0,j=0,k=0;
if ((fp=fopen("hhhh.txt","r"))==NULL)
{
fprintf(stderr,"\n cuo wu\n");
exit (1);
}
fprintf(stdout,"\n",&fp);
while (fgets(a,sizeof(a),fp))
{ k++;
printf("%s\n",a);
}
for (p=a;*p!=0;p++)
{ if (*p==32)
i-=1;
i++;
}
printf("hangshu:%d\n",k);
printf("diyihang:%d\n",i-j);
fclose(fp);
} 展开
#include<stdlib.h>
#define BA 1000
int main()
{
FILE *fp;
char a[BA],*p;
int i=0,j=0,k=0;
if ((fp=fopen("hhhh.txt","r"))==NULL)
{
fprintf(stderr,"\n cuo wu\n");
exit (1);
}
fprintf(stdout,"\n",&fp);
while (fgets(a,sizeof(a),fp))
{ k++;
printf("%s\n",a);
}
for (p=a;*p!=0;p++)
{ if (*p==32)
i-=1;
i++;
}
printf("hangshu:%d\n",k);
printf("diyihang:%d\n",i-j);
fclose(fp);
} 展开
1个回答
展开全部
亲,你这样试试,不知道合不合你的意:
#include<stdio.h>
#include<stdlib.h>
#define BA 1000
int main()
{
FILE *fp;
char a[BA],*p;
int i=0,j=0,k=0;
if ((fp=fopen("hhhh.txt","r"))==NULL)
{
fprintf(stderr,"\n cuo wu\n");
exit (1);
}
fprintf(stdout,"\n",&fp);
while (fgets(a,sizeof(a),fp))
{ k++;
if(k==1)
{
for (p=a;*p!=0;p++)
{ if (*p==32)
i-=1;
i++;
}
}
printf("%s\n",a);
}
printf("hangshu:%d\n",k);
printf("diyihang:%d\n",i-j);
fclose(fp);
system("PAUSE");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define BA 1000
int main()
{
FILE *fp;
char a[BA],*p;
int i=0,j=0,k=0;
if ((fp=fopen("hhhh.txt","r"))==NULL)
{
fprintf(stderr,"\n cuo wu\n");
exit (1);
}
fprintf(stdout,"\n",&fp);
while (fgets(a,sizeof(a),fp))
{ k++;
if(k==1)
{
for (p=a;*p!=0;p++)
{ if (*p==32)
i-=1;
i++;
}
}
printf("%s\n",a);
}
printf("hangshu:%d\n",k);
printf("diyihang:%d\n",i-j);
fclose(fp);
system("PAUSE");
return 0;
}
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询