涉及将文件读入 C 中的 char 数组的警告

Warnings involving reading file into char array in C

我有编译和打印文件数据的代码,但它充满了警告,我无法修复它们。

void batchMode(char **c) {

     char *batchBuffer = NULL;
     size_t batchSize = 0;

     FILE *fp = fopen(c, "r");

     fseek(fp, 0, SEEK_END);
     batchSize = ftell(fp);

     rewind(fp);

     batchBuffer = malloc((batchSize + 1) * sizeof(*batchBuffer));

     fread(batchBuffer, batchsize, 1, fp);

     batchBuffer[batchSize] = 0;

     printf("%s\n", batchBuffer);

}

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

    if (argc == 2)
          batchMode(&argv[1][0]);

     return 0;

}

警告包括: 从不兼容的指针类型传递 batchmode 参数 1

batchMode(&argv[1][0]);

预期 'char **' 但参数类型为 char *

void batchMode(char **c)

从不兼容的指针类型传递 fopen 的参数 1

FILE *fp = fopen(c, "r");

预期 'const char * restrict' 但参数类型为 char **

FILE *fopen (const char *_restrict_filename

  • 您可能想要更改 batchMode 的方法签名以使用字符指针而不是指向指针的指针。
  • 因此,您应该将函数调用调整为 batchMode(argv[1]); 以将第一个程序参数作为参数传递
  • fread的第二个参数必须是batchSize而不是batchsize(注意大写S
  • 由于batchBuffer是动态分配的,所以应该在不需要的后面加一个free (batchBuffer);

因此,您稍加修改的代码可能如下所示:

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


void batchMode(const char *c) {
    char *batchBuffer = NULL;
    size_t batchSize = 0;
    FILE *fp = fopen(c, "r");
    fseek(fp, 0, SEEK_END);
    batchSize = ftell(fp);
    rewind(fp);

    batchBuffer = malloc((batchSize + 1) * sizeof(*batchBuffer));

    fread(batchBuffer, batchSize, 1, fp);

    batchBuffer[batchSize] = 0;

    printf("%s\n", batchBuffer);

    free(batchBuffer);
}

int main(int argc, char **argv) {
    if (argc == 2)
        batchMode(argv[1]);

    return 0;
}

对 OP 代码提出以下更改:

  1. 干净地编译
  2. 执行所需的功能
  3. 正确检查错误
  4. 使用完后正确释放分配的内存

注意:函数:'perror()'将系统认为上次失败的错误信息和文本原因传递给'stderr'

现在,提议的修改,附上评论

#include <stdio.h>     //<-- missing
#include <stdlib.h>    //<-- missing


//void batchMode(char **c) {        //<-- needs pointer to a string, not a single character
         //<-- suggest using meaningful name, like: 'filename'
void batchMode( char *fileName ) {

     //char *batchBuffer = NULL;    //<-- limit scope and keep with 'setter'
     //size_t batchSize = 0;       //<-- ftell() returns a 'long', not a unsigned long
     //long batchSize = 0;         //<-- limit scope and keep with 'setter'

     FILE *fp = fopen( fileName, "r");        //<-- check returned value for success (!=NULL)
     if( ! fp )
     {
         perror( "fopen failed" );
         exit( EXIT_FAILURE );
     }

     //fseek(fp, 0, SEEK_END);         //<-- returns an 'int', check for success (!=-1)
      long batchSize;
     if( (batchSize = fseek( fp, 0, SEEK_END )) == -1 )
     {
         perror( "fseek for end of file failed" );
         exit( EXIT_FAILURE );
     }

     batchSize = ftell(fp);               //<-- check the returned value for an error indication (-1)
                                     //<-- returns a 'long', not a 'unsigned long'
     if( batchSize == -1 )
     {
         perror( "ftell failed" );
         exit( EXIT_FAILURE );
     }

     rewind(fp);                   //<-- does not have error checking, suggest: using 'fseek(fp, 0, SEEK_SET )'

    // batchBuffer = malloc((batchSize + 1) * sizeof(*batchBuffer));  //<-- 'sizeof(*batchBuffer)' is the size of a char pointer 4 or 8 depending on the underlying hardware architecture and certain compile options
                                         //<-- check for success, returned value (!=NULL)
     char * batchBuffer = malloc( (size_t)batchSize+1);
     if( ! batchBuffer )
     {
         perror( "malloc failed" );
         exit( EXIT_FAILURE );
     }

     //fread(batchBuffer, batchsize, 1, fp);   // incorrect capitalization of batchSize
     if( fread(batchBuffer, (size_t)batchSize, 1, fp) != 1 )    //<-- if returned value != third parameter, then error occurred
     {
         perror( "fread failed" );
         exit( EXIT_FAILURE );
     }

     batchBuffer[batchSize] = 0;

     printf("%s\n", batchBuffer);

     free( batchBuffer );      //<-- to avoid memory leak
}


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

     //if (argc == 2)               //<--  handle error first
     if( argc != 2 )
     {
         fprintf( stderr, "USAGE: %s <fileName>\n", argv[0] );
         exit( EXIT_FAILURE );
     }

     //batchMode(&argv[1][0]);   //<-- send pointer to first command line parameter, not a single character
     batchMode( argv[1] );

     return 0;
}