C程序读取文件时输出错误

C程序读取文件时输出错误,c,C,大家好,我写了这个程序,目的是打开一个文件,读取其中有多少个字符,然后用最多和最少的字符打印这行。我把它分成两个函数,一个用于最大的行,一个用于最小的行。最大的行函数工作正常,但最小的一个函数的输出错误。下面是代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> char

大家好,我写了这个程序,目的是打开一个文件,读取其中有多少个字符,然后用最多和最少的字符打印这行。我把它分成两个函数,一个用于最大的行,一个用于最小的行。最大的行函数工作正常,但最小的一个函数的输出错误。下面是代码:

       #include <stdio.h>
       #include <stdlib.h>
       #include <string.h>
       #include <conio.h>
       char f_view[150];
       void ShowResults();
       int leastsymbols();
       int mostsymbols();
       int main(){
       ShowResults();
       return 0;
       }
       int mostsymbols(){
       FILE *fp;
         fp=fopen(f_view, "r");
         if(fp==NULL){
         printf("Error\n");
         exit(-1);
         }
        int lineNO=1;
        int c;
        int currCount=0;
        int highestCount=0;
        int highestline=0;
        while ((c = getc(fp)) != EOF){
        if (c == '\n') {
        currCount=0;
            lineNO++;
        }

       if (c != '\n' && c != '\t' && c!= ' ') {
       currCount++;
       if(currCount>highestCount){
            highestCount=currCount;
                if(lineNO>highestline){
                    highestline=lineNO;
                    }
                    }
         }
         }
        fclose(fp);
        return highestline;
       }
   int leastsymbols()
    {
    FILE *fp;
    fp = fopen(f_view, "r");
    if (fp == NULL)
    {
    printf("Could not open file \n");
    exit(-1);
    }
    int c;
    int lineNO = 1;
    int currCount=0;
    int leastLine=0;
    int leastCount=1000;//assuming that a line in a file can not be longer 
                        //than 1000 characters
    while ((c = getc(fp)) != EOF){
    if (c == '\n'){

    currCount = 0;
    lineNO++;
    }
    if (c != '\n' && c != '\t' && c!= ' ') {

    currCount++;

    }
    if(currCount<leastCount){
     leastCount=currCount;
      leastLine=lineNO;
        }
    }

    fclose(fp);
    return leastLine;

    }
    void ShowResults()
    {
    FILE *fptr;
    char *fix;
    char c;
    char openFile[1024];
    printf("Type the destination to the *.c file or the file name.\n");
          //the user has to enter a .C file
      while(f_view[strlen(f_view) - 2] != '.' && f_view[strlen(f_view) - 1] 
      != 'c')
    {
      fgets(f_view, 150, stdin);
      fix = strchr(f_view, '\n');
      if(fix != 0)
             *fix = 0;
    }
    if((fptr = fopen(f_view, "r")) == NULL)
    {
      printf("Cannot open file !\n");
      exit(-1);
    }
    int highestLine;
    int lowestLine;
    while (fgets(openFile, 1024, fptr))
    {
      highestLine=mostsymbols();
      lowestLine=leastsymbols();

    }
    printf("Line %d has the most symbols.\n",highestLine);
    printf("Line %d has the least symbols.\n",lowestLine);

    fclose(fptr);
    return ;
    }
#包括
#包括
#包括
#包括
char f_视图[150];
void ShowResults();
int leastsymbols();
int mostsymbols();
int main(){
ShowResults();
返回0;
}
int mostsymbols(){
文件*fp;
fp=fopen(f_视图,“r”);
如果(fp==NULL){
printf(“错误\n”);
出口(-1);
}
int lineNO=1;
INTC;
int currCount=0;
int highestCount=0;
int-highestline=0;
而((c=getc(fp))!=EOF){
如果(c=='\n'){
currCount=0;
lineNO++;
}
如果(c!='\n'&&c!='\t'&&c!=''){
currCount++;
如果(当前计数>最高计数){
最高计数=当前计数;
如果(行号>最高位行){
最高线=线号;
}
}
}
}
fclose(fp);
返回最高线;
}
int leastsymbols()
{
文件*fp;
fp=fopen(f_视图,“r”);
如果(fp==NULL)
{
printf(“无法打开文件\n”);
出口(-1);
}
INTC;
int lineNO=1;
int currCount=0;
int leastLine=0;
int leastCount=1000;//假设文件中的一行不能更长
//超过1000个字符
而((c=getc(fp))!=EOF){
如果(c=='\n'){
currCount=0;
lineNO++;
}
如果(c!='\n'&&c!='\t'&&c!=''){
currCount++;
}

如果(currCount转到下一行时将此检查移动到

 if(currCount<leastCount){
           leastCount=currCount;
           leastLine=lineNO;
    }

if(currCount我修复了我的程序,谢谢。:)

while((c=getc(fp))!=EOF){

如果(c=='\n'&&currCountif(c=='\n'){if(currcount)我做到了。谢谢!
    while ((c = getc(fp)) != EOF){
    if(c == '\n' && currCount<leastCount){
    leastCount=currCount;
    leastLine=lineNO;
    }
    if(c=='\n'){
    currCount = 0;
    lineNO++;
         }
        if (c != '\n' && c != '\t' && c!= ' ') {

    currCount++;

    }
    }