用于计算文件中行数的C函数

时间:2012-10-04 18:03:43

标签: c

当我尝试运行程序时,打印的行数错误。

LINES: 0

这是输出,虽然我的.txt文件中有五行

这是我的计划:

#include<stdio.h>
#include<stdlib.h>

int countlines(char *filename);

void main(int argc, char *argv[])
{
  printf("LINES: %d\n",countlines(argv[1]));         
}


int countlines(char *filename)
{
  // count the number of lines in the file called filename                                    
  FILE *fp = fopen(filename,"r");
  int ch=0;
  int lines=0;

  if (fp == NULL);
  return 0;

  lines++;
  while ((ch = fgetc(fp)) != EOF)
    {
      if (ch == '\n')
    lines++;
    }
  fclose(fp);
  return lines;
}

我确信这是一个简单的错误,但我是编程新手。任何帮助将不胜感激。

7 个答案:

答案 0 :(得分:27)

while(!feof(fp))
{
  ch = fgetc(fp);
  if(ch == '\n')
  {
    lines++;
  }
}

但请注意:Why is “while ( !feof (file) )” always wrong?

答案 1 :(得分:4)

你宣布

int countlines(char *filename)

采用char *参数。

你这样称呼它

countlines(fp)

传入文件*。

这就是你得到编译错误的原因。

您可能应该将第二行更改为

countlines("Test.txt")

因为您在 countlines

中打开文件

您当前的代码正在尝试在两个不同的位置打开该文件。

答案 2 :(得分:3)

您正在打开一个文件,然后将文件指针传递给只需要文件名来打开文件本身的函数。您可以简化您的呼叫;

void main(void)
{
  printf("LINES: %d\n",countlines("Test.txt"));
}
编辑:你正在改变这个问题所以很难回答;一开始你的更改为main()错误,你忘了第一个参数是argc,所以它崩溃了。现在你遇到了问题;

if (fp == NULL);   // <-- note the extra semicolon that is the only thing 
                   //     that runs conditionally on the if 
  return 0;        // Always runs and returns 0

总是会返回0.删除那个额外的分号,你应该得到合理的数量。

答案 3 :(得分:1)

你有一个;在if的末尾。 变化:

  if (fp == NULL);
  return 0;

  if (fp == NULL) 
    return 0;

答案 4 :(得分:0)

对于导致分段错误的原因,我没有看到任何明显的信息。我唯一的怀疑是你的代码在运行时希望得到一个文件名作为参数,但是如果你没有传递它,那么无论如何都会尝试引用它。

argv[1]不存在时,导致分段错误。 在尝试引用参数之前检查参数的数量通常是一种好习惯。您可以使用main()的以下函数原型来执行此操作,并检查argc是否更大比1(简单来说,它将指示argv中的数字条目)。

int main(int argc, char** argv)

找出导致段错误的一般原因的最佳方法是使用调试器。如果您在Visual Studio中,请在主函数的顶部放置一个断点,然后在启动程序时选择 Run with debugging 而不是“Run without debugging”。它将停止在顶部执行,让您逐行执行直到看到问题为止。

如果您使用的是Linux,则只需获取核心文件(名称中将包含“core”)并使用gdb(GNU Debugger)加载。它可以为您提供堆栈转储,它会直接指向导致分段错误发生的行。

编辑:我看到你改变了你的问题和代码。所以这个答案可能已经不再有用,但我会留下它,因为它是一个很好的建议,并且看看我是否可以解决修改过的问题,不久之后。)

答案 5 :(得分:0)

这是我的功能

char *fileName = "input-1.txt";
countOfLinesFromFile(fileName);

void countOfLinesFromFile(char *filename){
FILE* myfile = fopen(filename, "r");
int ch, number_of_lines = 0;
do
{
    ch = fgetc(myfile);
    if(ch == '\n')
        number_of_lines++;
}
while (ch != EOF);
if(ch != '\n' && number_of_lines != 0)
    number_of_lines++;
fclose(myfile);
printf("number of lines in  %s   = %d",filename, number_of_lines);

}

答案 6 :(得分:0)

这是在C / C ++中的完整实现

#include <stdio.h>

void lineCount(int argc,char **argv){

        if(argc < 2){
             fprintf(stderr,"File required");
             return;
        }
        FILE *fp = fopen(argv[1],"r");



        if(!fp){
            fprintf(stderr,"Error in opening file");
            return ;      
        }

        int count = 1; //if a file open ,be it empty, it has atleast a newline char
        char temp;

        while(fscanf(fp,"%c",&temp) != -1){
                if(temp == 10) count++;
        }

        fprintf(stdout,"File has %d lines\n",count);
   }

int main(int argc,char **argv){

        lineCount(argc,argv);
        return 0;
}
https://github.com/KotoJallow/Line-Count/blob/master/lineCount.c