Printf and fprintf prints only first argument
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
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)".
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);
精彩评论