将结构中的int转换为C中的字符串
我试图将结构中的int值转换为字符串,以便将它们写入ppm文件的头中 结构的定义如下:将结构中的int转换为C中的字符串,c,C,我试图将结构中的int值转换为字符串,以便将它们写入ppm文件的头中 结构的定义如下: typedef struct { int width; int height; int maxColourVal; FILE *ppmFilePointer; } PpmStruct; 函数创建新的ppm文件: PpmStruct *newWritePpm(const char *filename, PpmStruct *parentPpm){ FILE *outfi
typedef struct {
int width;
int height;
int maxColourVal;
FILE *ppmFilePointer;
} PpmStruct;
函数创建新的ppm文件:
PpmStruct *newWritePpm(const char *filename, PpmStruct *parentPpm){
FILE *outfile = fopen(filename, "w");
if (!outfile){
printf("Unable to open '%s'\n", filename);
exit(1);
}
PpmStruct *newPpm;
newPpm = (PpmStruct *)malloc(sizeof(PpmStruct));
/* Populating ppm struct*/
(*newPpm).width = (*parentPpm).width;
(*newPpm).height = (*parentPpm).height;
(*newPpm).maxColourVal = (*parentPpm).maxColourVal;
(*newPpm).ppmFilePointer = outfile;
/* writing outfile ppm header to file*/
fputs("P6\n", outfile);
fputs((*parentPpm).width, outfile);
fputs(" ", outfile);
fputs((*newPpm).height, outfile);
fputs("\n", outfile);
fputs((*newPpm).maxColourVal, outfile);
fputs("\n", outfile);
/* leaves pointer at start of binary pixel data section */
return(newPpm);
}
编译时,我从编译器中得到了几个类似的警告:
ppmCommon.h: In function ‘newWritePpm’:
ppmCommon.h:75:8: warning: passing argument 1 of ‘fputs’ makes pointer from integer without a cast [-Wint-conversion]
fputs((*parentPpm).width, outfile);
fputs用于编写字符串。parentPpm->width是一个整数。例如,您需要输出ASCII十进制整数。最简单的方法是对整个标头使用单个fprintf调用:
fprintf(outfile, "P6\n%d %d\n%d\n",
parentPpm->width, newPpm->height, newPpm->maxColourVal);
*newPpm.width->newPpm->width等等。作为旁注,%d仅适用于int值;对于long int,您需要使用%ld;对于long long int,您需要使用%lld。。。