我寫了一個c代碼來計算文件中的字符數,位數和行數。不幸的是,行數沒有給出確切的計數。我寫下了下面的代碼。計算文件中的行數
#include<stdio.h>
void scan();
FILE *fp;
int numbercount=0,textcount=0,spacecount=0,newlinecount=0,specialcount=0;
int main(int argc,char *argv[])
{
if(argc<2)
{
printf("\n Enter the filename through the command line ! ");
}
else
{
fp=fopen(argv[1],"r");
if(fp==NULL)
printf("\n Cannot Open the file ");
else
scan();
}
}
void scan()
{
char ch;
while(1)
{
ch=fgetc(fp);
if((ch>=65 && ch<=90)||(ch>=97 && ch<=122))
{
textcount++;
}
else if(ch>=48&&ch<=57)
{
numbercount++;
}
else if(ch==','||ch=='!'||ch=='?'||ch=='.')
{
specialcount++;
}
else if(ch==' ')
{
spacecount++;
}
else if(ch=='\n')
{
newlinecount++;
}
else if(ch==EOF)
break;
}
printf("\n The count of charecters in the text = %d ",textcount);
printf("\n The count of numbers in the text = %d ",numbercount);
printf("\n The count of special charecters in the text = %d",specialcount);
printf("\n The count of newlines = %d ",newlinecount);
printf("\n The number of spaces = %d \n",spacecount);
}
我已經給輸入文本文件內容如下http://pastebin.com/GXVdqfzT,代碼給出了線數爲6,而不是11.是否有計算行數的適當方式。
...文件似乎有一個6的行數,如pastebin中所示。我錯過了什麼嗎? – Bart
換行和換行符有所不同。當空間用完時,請繼續打包。 – ChiefTwoPencils
感謝您的輸入 –