Printf 和 fprintf 仅打印第一个参数

发布于 2024-12-05 15:27:54 字数 1376 浏览 1 评论 0原文

我有一个 printf 无法理解的问题。这是我第一次遇到这个问题,所以我确信这很天真,但无论如何,我无法自己解决它......也许只是因为我累了:fprintf(并且我发现printf 也是如此)正确地仅打印第一个参数,从第二个参数开始,它将仅打印数字“0”和字符串“(null)”

这是相关代码:

#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void printInputStream(MatePair* inputStream, char* leftFile, char* rightFile){

    MatePair* iterator = inputStream;
    FILE* outLeft = fopen(leftFile, "w");
    FILE* outRight = fopen(rightFile, "w");


    while (iterator->leftRow != MATEPAIR_STOP){

        fprintf(outLeft, "%d: \n", iterator->leftRow);
        fprintf(outLeft, "%s \n", iterator->leftDNA);
        fprintf(outLeft, "%d: %s \n", iterator->leftRow, iterator->leftDNA);

        iterator++;
    }

    fclose(outLeft);
    fclose(outRight);

}

这是输出的开头:

48: 
NAATAGACCTATATCCTGTACCCAAACAGAAGACAGAGGATTAACCAAACTCTT 
48: (null) 
44: 
NTAGCCATCTTAGACACATGAATATCTTGGGTCACAACTCATACCTCAACAAAA 
44: (null) 
40: 
NAAAATAAGGGGTATACTCGCTTCGGGGCCCCATTTGGCCTCCAAAAGGGGGCG 
40: (null) 
36: 
NTCTATCTTGCTCGAGAGAAAGGGTTGCCTTAGGGTTTTTTGGGGGGGGCTGTA 
36: (null) 
32: 
NCTATAGAAATTTCCCATACCAACTAGACATTTATCTTCCTGTTTTTTTCCGCC 
32: (null) 

如您所见,我打印每个数组成员两次:每个参数一次,两个参数一起一次。数据很好,事实上,使用第一种方法一切都可以,使用第二种方法只打印第一个参数。 有什么想法吗? 提前致谢

I have a problem I can't grasp with printf. It's the first time ever I have this problem, so I'm sure it's something naive, but no matter what, I can't solve it myself... maybe it's just because I'm tired: fprintf (and i've found it's true also for printf) correctly prints only the first argument, from the second it will print only "0" for numbers and "(null)" for strings

Here's the relevant code:

#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void printInputStream(MatePair* inputStream, char* leftFile, char* rightFile){

    MatePair* iterator = inputStream;
    FILE* outLeft = fopen(leftFile, "w");
    FILE* outRight = fopen(rightFile, "w");


    while (iterator->leftRow != MATEPAIR_STOP){

        fprintf(outLeft, "%d: \n", iterator->leftRow);
        fprintf(outLeft, "%s \n", iterator->leftDNA);
        fprintf(outLeft, "%d: %s \n", iterator->leftRow, iterator->leftDNA);

        iterator++;
    }

    fclose(outLeft);
    fclose(outRight);

}

Here's the beginning of the output:

48: 
NAATAGACCTATATCCTGTACCCAAACAGAAGACAGAGGATTAACCAAACTCTT 
48: (null) 
44: 
NTAGCCATCTTAGACACATGAATATCTTGGGTCACAACTCATACCTCAACAAAA 
44: (null) 
40: 
NAAAATAAGGGGTATACTCGCTTCGGGGCCCCATTTGGCCTCCAAAAGGGGGCG 
40: (null) 
36: 
NTCTATCTTGCTCGAGAGAAAGGGTTGCCTTAGGGTTTTTTGGGGGGGGCTGTA 
36: (null) 
32: 
NCTATAGAAATTTCCCATACCAACTAGACATTTATCTTCCTGTTTTTTTCCGCC 
32: (null) 

As you can see I print every member of the array twice: once per argument and both arguments together. The data is fine, in fact with the first method it's all ok, with the second one only the first argument is printed.
Any idea?
Thanks in advance

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

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

发布评论

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

评论(2

独留℉清风醉 2024-12-12 15:27:54

下面的行(带有演员表)“有效”吗?

fprintf(outLeft, "%d: %s \n", (int)iterator->leftRow, iterator->leftDNA);

我怀疑 iterator->leftRow 不是 int 类型(或转换为 int 的某种较小类型)自动)。如果我是对的,你会调用未定义行为;在第一种情况(单独的语句)中,没有明显的“不当行为”(运气不好),在第二种情况中,“不当行为”是打印“(NULL)”。

Does the following line, with a cast, "work"?

fprintf(outLeft, "%d: %s \n", (int)iterator->leftRow, iterator->leftDNA);

I suspect iterator->leftRow is not of int type (or some smaller type that gets converted to int automagically). If I'm right you invoke Undefined Behaviour; in the 1st case (the separate statements) there's no apparent "misbehaviour" (bad luck), in the 2nd case the "misbehaviour" is to print "(NULL)".

静若繁花 2024-12-12 15:27:54

哪种数据类型具有 iterator->leftRow?您是否尝试过这样做(即假设它很长)。

fprintf(outLeft, "%ld: %s \n", iterator->leftRow, iterator->leftDNA);

Which data type has iterator->leftRow? Have you tried to do (i.e. assume it to be a long).

fprintf(outLeft, "%ld: %s \n", iterator->leftRow, iterator->leftDNA);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文