need help to understand flaw in my program

时间:2022-01-26 19:00:19

I am programming in C and I start to do some basic programming like wordcount in files, but unfortunately I get flaw in my program's execution. The gcc compiler displays such a warning:

我在C编程,我开始在文件中做一些基本的编程,如wordcount,但不幸的是我的程序执行中存在缺陷。 gcc编译器显示这样一个警告:

test.c: In function ‘main’:
test.c:11: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast
/usr/include/stdio.h:269: note: expected ‘const char * __restrict__’ but argument is of type ‘char’

Line 11 is the line with the if statement

第11行是if语句的行

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define FAIL -1 
#define SUCCESS 1

int main (char *filename) {
    FILE *fp;
    char c;
    int wordcount = 0;
    if ((fp = fopen(*filename,"r")) == NULL)
        return FAIL;
    while (!feof(fp))
    {
        while(!isalpha(fgetc(fp)))
        {
            wordcount++;
        }
    }
    printf("wordcount: %d",wordcount);  
    fclose(fp);
    return SUCCESS;
}

4 个解决方案

#1


5  

The asterisk when applied before a pointer in C dereferences the pointer, i.e. it evaluates to whatever the pointer points at. You don't want that, you want:

在C中指针之前应用的星号取消引用指针,即它评估指针指向的任何内容。您不希望如此,您想要:

if ((fp = fopen(filename,"r")) == NULL)

Otherwise you're passing a single character (the first character in filename) to fopen(), which expects a pointer to a 0-terminated array of characters (aka "a string").

否则,你将一个字符(文件名中的第一个字符)传递给fopen(),它需要一个指向0终止字符数组(也就是“字符串”)的指针。

#2


0  

int main( int argc, char **argv )
{
  FILE *fp;
  char *path;
  path = argc > 1 ? argv[1] : "default"
  fp = fopen( path, "r" );
  if( fp == NULL ) {
    perror( path );
    exit( EXIT_FAILURE );
  }
  ...

#3


0  

main has to be declared as taking either no argument or (int argc, char* argv[]). And it has to return an int in [0,255], typically either EXIT_SUCCESS or EXIT_FAILURE.

main必须声明为无参数或(int argc,char * argv [])。它必须在[0,255]中返回一个int,通常是EXIT_SUCCESS或EXIT_FAILURE。

After that, enable warnings in your compiler (gcc -Wall -Werror) and see how it goes.

之后,在编译器中启用警告(gcc -Wall -Werror)并查看其运行方式。

#4


0  

By prefixing filename with a '*' you're passing the data filename points to rather than the address where the data is stored - should be:

通过在文件名前加上'*',你将数据文件名点传递给而不是存储数据的地址 - 应该是:

if ((fp = fopen(filename,"r")) == NULL)

And main only takes the arguments argc and argv. If you're passing the filename as the first argument on the command line, then:

main只接受参数argc和argv。如果您将文件名作为命令行的第一个参数传递,则:

int main (int argc, char* argv[])
{
   FILE *fp;
   char c;
   int wordcount = 0;
   if (argc<1) {
      fprintf(stderr, "need a filename");
      return FAIL;
   }
   if ((fp = fopen(argv[1],"r")) == NULL) {
   ....

#1


5  

The asterisk when applied before a pointer in C dereferences the pointer, i.e. it evaluates to whatever the pointer points at. You don't want that, you want:

在C中指针之前应用的星号取消引用指针,即它评估指针指向的任何内容。您不希望如此,您想要:

if ((fp = fopen(filename,"r")) == NULL)

Otherwise you're passing a single character (the first character in filename) to fopen(), which expects a pointer to a 0-terminated array of characters (aka "a string").

否则,你将一个字符(文件名中的第一个字符)传递给fopen(),它需要一个指向0终止字符数组(也就是“字符串”)的指针。

#2


0  

int main( int argc, char **argv )
{
  FILE *fp;
  char *path;
  path = argc > 1 ? argv[1] : "default"
  fp = fopen( path, "r" );
  if( fp == NULL ) {
    perror( path );
    exit( EXIT_FAILURE );
  }
  ...

#3


0  

main has to be declared as taking either no argument or (int argc, char* argv[]). And it has to return an int in [0,255], typically either EXIT_SUCCESS or EXIT_FAILURE.

main必须声明为无参数或(int argc,char * argv [])。它必须在[0,255]中返回一个int,通常是EXIT_SUCCESS或EXIT_FAILURE。

After that, enable warnings in your compiler (gcc -Wall -Werror) and see how it goes.

之后,在编译器中启用警告(gcc -Wall -Werror)并查看其运行方式。

#4


0  

By prefixing filename with a '*' you're passing the data filename points to rather than the address where the data is stored - should be:

通过在文件名前加上'*',你将数据文件名点传递给而不是存储数据的地址 - 应该是:

if ((fp = fopen(filename,"r")) == NULL)

And main only takes the arguments argc and argv. If you're passing the filename as the first argument on the command line, then:

main只接受参数argc和argv。如果您将文件名作为命令行的第一个参数传递,则:

int main (int argc, char* argv[])
{
   FILE *fp;
   char c;
   int wordcount = 0;
   if (argc<1) {
      fprintf(stderr, "need a filename");
      return FAIL;
   }
   if ((fp = fopen(argv[1],"r")) == NULL) {
   ....