Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将结构中的int转换为C中的字符串_C - Fatal编程技术网

将结构中的int转换为C中的字符串

将结构中的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

我试图将结构中的int值转换为字符串,以便将它们写入ppm文件的头中

结构的定义如下:

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。。。