gpt4 book ai didi

c - 打印到文件时如何正确对齐

转载 作者:行者123 更新时间:2023-11-30 18:01:18 25 4
gpt4 key购买 nike

我有这个结构,我需要将其元素写入文本文件

struct Stage3ADisplay
{
int nSlot;
char *Item;
char *Type;
int nIndex;
unsigned char attributesMD[17]; //the last character is \0
unsigned char contentsMD[17]; //only for regular files - //the last character is \0
};

buffer = malloc(sizeof(Stage3ADisplayVar[nIterator]->nSlot) + sizeof(Stage3ADisplayVar[nIterator]->Item) + sizeof(Stage3ADisplayVar[nIterator]->Type) + sizeof(Stage3ADisplayVar[nIterator]->nIndex) + sizeof(Stage3ADisplayVar[nIterator]->attributesMD) + sizeof(Stage3ADisplayVar[nIterator]->contentsMD) + 1);

sprintf (buffer,"%d %s %s %d %x %x",Stage3ADisplayVar[nIterator]->nSlot, Stage3ADisplayVar[nIterator]->Item,Stage3ADisplayVar[nIterator]->Type,Stage3ADisplayVar[nIterator]->nIndex,Stage3ADisplayVar[nIterator]->attributesMD,Stage3ADisplayVar[nIterator]->contentsMD);

如何确保文件中的行正确对齐。谢谢。

最佳答案

如果您的问题与 sprintf 相关,

改变

 sprintf (buffer,"%d %s %s %d %x %x"

 sprintf (buffer,"%22.22d %22.22s %22.22s %22.22d %22.22x %22.22x"

看看 %nn.nn 是如何工作的。

关于c - 打印到文件时如何正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9883528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com