结构和fprintf()

时间:2014-04-22 23:52:03

标签: c printf

我想知道是否可以在结构中使用fprintf(),因为我知道你不能在结构中使用“%”。

struct blackhole_register
{
int userID;
float blackhole_Mass;
char blackhole_ID[5];
char name_First[11];
char name_Last[16];
};
int main ()
{
struct blackhole_register input;
struct blackhole_register output;

FILE *blackhole_file;

if ((blackhole_file = fopen("Holter.txt","w")) == NULL)
{
    printf("File location not found, the program will now end\n");
}
else
    printf("Schwarzschild Radius Application by Jonathan Holter\n\n");

    printf("\nFirst Name: ");
    fgets(input.name_First,11,stdin);

    printf("\nLast Name: ");
    fgets(input.name_Last,16,stdin);

    printf("\nUser ID: ");
    fgets(input.userID,4,stdin);

    printf("\nBlack Hole Name/ID: ");
    fgets(input.blackhole_ID,20,stdin);

    printf("\nBlack Hole Mass (Solar Masses): ");
    fgets(input.blackhole_Mass,3,stdin);

这就是我到目前为止,任何帮助都会很精彩!!

1 个答案:

答案 0 :(得分:2)

%serialize-my-struct格式字符串中没有魔术printf标记。您需要分别在结构中的printf每个字段。考虑编写函数int print_blackhole_register(FILE*, const blackhole_register*)