fprintf 在开头打印一堆 NULL

发布于 2025-01-10 07:36:49 字数 1107 浏览 0 评论 0原文

typedef struct record
{
    float field1;
    char *name;
} record;

void printRecordsTo(record **s, char *fileName, int size)
{
    initializePrinter(fileName);
    int i = 0;
    while (i < size)
    {
        printRecordToFile(s[i]);
        i++;
    }
    closePrinter();
}

static int delete = 0; // TODO: Remove
void printRecordToFile(record *r)
{
    if (!file)
    {
        puts("Printer not initialized");
        exit(1);
    }
    printf("%d %s", delete ++, toString(r)); // prints all correctly to stdout
    fprintf(file, "%s", toString(r));        // includes many NULLs
}

char *toString(record *s)
{
    float field1 = s->field1;
    int len = 4;
    char *field1Str = malloc(len + 1);
    snprintf(field1Str, len + 1, "%f", field1);
    char *name = s->name;
    char *buf = malloc((1024) * sizeof(char));
    snprintf(buf, 1023, "%s%s%s%s%s\n", "(", name, ", ", field1Str, ")");
    return buf;
}

我不明白为什么 fprintf 对于大文件(大小约为 1000 条记录)打印许多 NULL。该函数对于我使用的小文件(大小约为 20 条记录)运行良好。我针对正常的 printf 进行了测试,它可以正确打印记录的字符串表示形式。

typedef struct record
{
    float field1;
    char *name;
} record;

void printRecordsTo(record **s, char *fileName, int size)
{
    initializePrinter(fileName);
    int i = 0;
    while (i < size)
    {
        printRecordToFile(s[i]);
        i++;
    }
    closePrinter();
}

static int delete = 0; // TODO: Remove
void printRecordToFile(record *r)
{
    if (!file)
    {
        puts("Printer not initialized");
        exit(1);
    }
    printf("%d %s", delete ++, toString(r)); // prints all correctly to stdout
    fprintf(file, "%s", toString(r));        // includes many NULLs
}

char *toString(record *s)
{
    float field1 = s->field1;
    int len = 4;
    char *field1Str = malloc(len + 1);
    snprintf(field1Str, len + 1, "%f", field1);
    char *name = s->name;
    char *buf = malloc((1024) * sizeof(char));
    snprintf(buf, 1023, "%s%s%s%s%s\n", "(", name, ", ", field1Str, ")");
    return buf;
}

I can't figure out why many NULLs are being printed by the fprintf for large files (of size ~1000 records). The function works fine for the small files (of size ~20 records) I used. I tested it against normal printf which prints the string representations of records correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2025-01-17 07:36:49
  1. 从源代码中删除toString()函数,它是多余的。

  2. printRecordToFile()中使用以下语句

    printf ("%d (%s, %f)", delete++, r->name, r->field1);
    fprintf (file, "(%s, %f)", r->name, r->field1);
  1. 为结构属性/成员使用有意义的名称。

  2. 如果您仍然看到垃圾值,那么您可能需要使用调试器来找出结构数据被损坏的位置。根据现有信息,我们只能猜测。

  3. 使用Valgrind来跟踪内存错误和内存错误泄漏。对于 C 程序员来说,这是一个非常漂亮的工具。

  1. Drop toString() function from source, it's redundant.

  2. Use below statements instead in printRecordToFile()

    printf ("%d (%s, %f)", delete++, r->name, r->field1);
    fprintf (file, "(%s, %f)", r->name, r->field1);
  1. Use meaningful names for structure attributes/members.

  2. If you're still seeing garbage values, then perhaps you need to sit with a debugger to find out where structure data is being corrupted. With the info at hand we can only guess.

  3. Use Valgrind to trace memory error & leaks. It's a pretty nifty tool for C programmers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文