Printf and fprintf print only the first argument

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

Here is 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 each member of the array twice: once for each argument and both arguments together. The data is in order, in fact with the first method all this is normal, with the second only the first argument is output. Any ideas? thanks in advance

+4
source share
2 answers

Is the following line used with the product "work"?

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

I suspect that iterator->leftRow is not of type int (or some smaller type that automatically converts to int ). If I'm right, you call Undefined Behavior ; in the first case (separate statements) there is no obvious "wrong behavior" (failure), in the second case, "incorrect behavior" should print "(NULL)".

+8
source

What data type does iterator-> leftRow have? You tried to do (i.e. assume it is long).

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

All Articles