Printf 和 fprintf 仅打印第一个参数
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的行(带有演员表)“有效”吗?
我怀疑
iterator->leftRow
不是int
类型(或转换为int
的某种较小类型)自动)。如果我是对的,你会调用未定义行为;在第一种情况(单独的语句)中,没有明显的“不当行为”(运气不好),在第二种情况中,“不当行为”是打印“(NULL)”。Does the following line, with a cast, "work"?
I suspect
iterator->leftRow
is not ofint
type (or some smaller type that gets converted toint
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)".哪种数据类型具有 iterator->leftRow?您是否尝试过这样做(即假设它很长)。
Which data type has iterator->leftRow? Have you tried to do (i.e. assume it to be a long).