首页 > 解决方案 > 如何将描述符作为整数传递给函数?

问题描述

我需要计算文件中特定字符的出现次数,执行搜索的函数定义如下:

int countChar(int descriptor, char *charsearch);

在编译器时,我有以下错误:

program.c: In function ‘main’:
program.c:14:89: warning: passing argument 1 of ‘countChar’ makes integer from pointer without a cast [-Wint-conversion]
  printf("The character %s has been found %d times in the file %s.", argv[1], countChar(o_file, argv[1]), argv[2]);
program.c:4:19: note: expected ‘int’ but argument is of type ‘FILE *’
  int countChar(int descriptor, char *charsearch);
programa.c: In function ‘countChar’:
programa.c:25:15: warning: comparison between pointer and integer
  if ( leido == charsearch){

这是我的代码:

#include <stdio.h>

FILE *o_file;
int countChar(int descriptor, char *charsearch);

int main(int argc, char **argv){
    FILE *o_file = fopen(argv[2], "r");
    if (!o_file){
        printf("Cannot open file %s.", argv[2]);
        return 1;
    }else{
        printf("File %s open \n", argv[2]);
        printf("Searching for %s ...\n", argv[1]);
        printf("The character %s has been found %d times in the file %s.", argv[1], countChar(o_file, argv[1]), argv[2]);
    }
    fclose(o_file);
    printf("File %s closed", argv[2]);
}

int countChar(int descriptor, char *charsearch){
    int counting = 0;
    do {
        int leido = fgetc(o_file);
        if (leido != EOF){
            if ( leido == charsearch){
                counting ++;
            }
        }
    } while (!feof(o_file));
    return counting;
}

标签: c

解决方案


FILE*指针不是文件描述符,而是文件流。FILE*与您的函数声明不兼容。将函数更改为在第一个参数中采用 aFILE*而不是 an int,特别是因为该函数正在调用FILE*无论如何都作为输入的系统函数。

您还应该将第二个参数从 更改char*char,因为您只搜索单个字符,而不是字符串。

#include <stdio.h>

int countChar(FILE* f, char charsearch);

int main(int argc, char **argv){
    FILE *o_file = fopen(argv[2], "r");
    if (!o_file){
        printf("Cannot open file %s.", argv[2]);
        return 1;
    }
    printf("File %s open \n", argv[2]);
    printf("Searching for %s ...\n", argv[1]);
    printf("The character %c has been found %d times in the file %s.", argv[1][0], countChar(o_file, argv[1][0]), argv[2]);
    fclose(o_file);
    printf("File %s closed", argv[2]);
}

int countChar(FILE *f, char charsearch){
    int counting = 0;
    do {
        int leido = fgetc(f);
        if (leido != EOF){
            if (leido == charsearch){
                ++counting;
            }
        }
    } while (!feof(f));
    return counting;
}

如果不允许更改函数的签名,则必须使用open()代替fopen(),而read()不是代替fgetc()char*此外,在循环比较期间取消引用:

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int countChar(int descriptor, char* charsearch);

int main(int argc, char **argv){
    int fd = open(argv[2], O_RDONLY);
    if (fd == -1){
        printf("Cannot open file %s.", argv[2]);
        return 1;
    }
    printf("File %s open \n", argv[2]);
    printf("Searching for %s ...\n", argv[1]);
    printf("The character %s has been found %d times in the file %s.", argv[1], countChar(fd, argv[1]), argv[2]);
    close(fd);
    printf("File %s closed", argv[2]);
}

int countChar(int descriptor, char* charsearch){
    int counting = 0;
    char leido;
    while (read(descriptor, &leido, 1) == 1){
        if (leido == *charsearch){
            ++counting;
        }
    }
    return counting;
}

推荐阅读