C中涉及将文件读入字符数组的警告
我有从文件中编译和打印数据的代码,但它充满了警告,我无法修复它们C中涉及将文件读入字符数组的警告,c,file,pointers,warnings,C,File,Pointers,Warnings,我有从文件中编译和打印数据的代码,但它充满了警告,我无法修复它们 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 = mal
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*
无效批处理模式(字符**c)
从不兼容的指针类型传递fopen的参数1
文件*fp=fopen(c,“r”)
应为“const char*restrict”,但参数的类型为char**
文件*fopen(const char*_restrict_filename- 您可能希望将batchMode的方法签名更改为使用字符指针,而不是指向指针的指针
- 因此,您应该将函数调用调整为
以将第一个程序参数作为参数传递batchMode(argv[1]);
- fread的第二个参数必须是
,而不是batchSize
(注意大写字母batchSize
)S
- 由于batchBuffer是动态分配的,您应该在不再需要它之后添加一个
空闲(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;
}
#包括
#包括
无效批处理模式(常量字符*c){
char*batchBuffer=NULL;
大小\u t批次大小=0;
文件*fp=fopen(c,“r”);
fseek(fp,0,SEEK_END);
batchSize=ftell(fp);
倒带(fp);
batchBuffer=malloc((batchSize+1)*sizeof(*batchBuffer));
fread(batchBuffer,batchSize,1,fp);
batchBuffer[batchSize]=0;
printf(“%s\n”,batchBuffer);
自由(批处理缓冲区);
}
int main(int argc,字符**argv){
如果(argc==2)
batchMode(argv[1]);
返回0;
}
对OPs代码的以下建议更改:
#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;
}
\include//Count the stars/asterisks并对齐它们。我建议从阅读fopen()
规范开始,并确保它得到它想要的类型。这应该让您开始了。batchMode
的参数希望是字符指针(一颗星),而不是指向字符(两颗星)的指针:void batchMode(char*c)
。谢谢你们两位。由于命令行参数是双精度的,我被星星的数量弄糊涂了。这让我很困惑。发布的代码无法编译!在许多其他问题中,它缺少所需头文件所需的\include
语句。