警告:从不兼容的指针传递'fprintf'的参数2 输入
警告:格式不是字符串文字而没有格式参数
stdio.h:333:注意:预期'const char * restrict '但参数类型为'struct FILE *'
#include <stdio.h>
int main (void){
FILE *file;
unsigned char *buffer;
unsigned long fileLen;
//Open file
file = fopen("squirrel-gray.jpg", "rb");
if (!file)
{
fprintf(stderr, "Unable to open file %s", "squirrel_gray.jpg");
return;
}
//Get file length
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
//Allocate memory
buffer=(char *)(fileLen);
if (!buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
}
fclose(file);
FILE *image;
image = fopen("img.jpg", "w");
fprintf(image, file);
fclose(image);
}
答案 0 :(得分:4)
该行:
fprintf( image, file ) ;
file
是FILE*
而不是格式字符串。编译器警告意味着它所说的内容(并且是语义错误)。
据推测,您原本打算将buffer
的内容写入img.jpg?情况就是这样:
也许:
//Allocate memory
buffer = malloc( fileLen ) ;
if( buffer == NULL )
{
fprintf( stderr, "Memory error!" ) ;
}
else
{
// Read data from file
fread( buffer, 1, fileLen, file ) ;
// Write data to image (img.jpg)
FILE* image = fopen( "img.jpg", "wb" ) ;
if( image != NULL )
{
fwrite( buffer, 1, fileLen, image ) ;
fclose( image ) ;
}
}
fclose( file ) ;