cprintffile-handlingfputs

variation of output Between "fputs" and "printf"


I'm trying to make a program which reads a poem from a text file and displays it.

here is my code..

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *POEM;

    POEM = fopen("C:\\Users\\User\\Desktop\\Poem.txt", "r");

     char C ;

       do {

       C = getc(POEM);

       printf("%c", C);

    }while(C != EOF);


    return 0;
}

and it's output ..

From fairest creatures we desire increase,
That thereby beauty's rose might never die,
But as the riper should by time decease,
His tender heir might bear his memory:
But thou, contracted to thine own bright eyes,
Feed'st thy light's flame with self-substantial fuel,
Making a famine where abundance lies,
Thy self thy foe, to thy sweet self too cruel.
Thou that art now the world's fresh ornament,
And only herald to the gaudy spring,
Within thine own bud buriest thy content,
And tender churl mak'st waste in niggarding:
Pity the world, or else this glutton be,
To eat the world's due, by the grave and thee. 
Process returned 0 (0x0)   execution time : 0.250 s
Press any key to continue.

The code is working but, when i use fputs(&C, stdout) instead of printf("%c", C) it prints out this..

F`)═u( "r`)═u( "o`)═u( "m`)═u( " `)═u( "f`)═u( "a`)═u( "i`)═u( "r`)═u( "e`)═u( "s`)═u( "t`)═u( " `)═u( "c`)═u( "r`)═u( "e`)═u( "a`)═u( "t`)═u( "u`)═u( "r`)═u( "e`)═u( "s`)═u( " `)═u( "w`)═u( "e`)═u( " `)═u( "d`)═u( "e`)═u( "s`)═u( "i`)═u( "r`)═u( "e`)═u( " `)═u( "i`)═u( "n`)═u( "c`)═u( "r`)═u( "e`)═u( "a`)═u( "s`)═u( "e`)═u( ",`)═u( "
`)═u( "T`)═u( "h`)═u( "a`)═u( "t`)═u( " `)═u( "t`)═u( "h`)═u( "e`)═u( "r`)═u( "e`)═u( "b`)═u( "y`)═u( " `)═u( "b`)═u( "e`)═u( "a`)═u( "u`)═u( "t`)═u( "y`)═u( "'`)═u( "s`)═u( " `)═u( "r`)═u( "o`)═u( "s`)═u( "e`)═u( " `)═u( "m`)═u( "i`)═u( "g`)═u( "h`)═u( "t`)═u( " `)═u( "n`)═u( "e`)═u( "v`)═u( "e`)═u( "r`)═u( " `)═u( "d`)═u( "i`)═u( "e`)═u( ",`)═u( "
`)═u( "B`)═u( "u`)═u( "t`)═u( " `)═u( "a`)═u( "s`)═u( " `)═u( "t`)═u( "h`)═u( "e`)═u( " `)═u( "r`)═u( "i`)═u( "p`)═u( "e`)═u( "r`)═u( " `)═u( "s`)═u( "h`)═u( "o`)═u( "u`)═u( "l`)═u( "d`)═u( " `)═u( "b`)═u( "y`)═u( " `)═u( "t`)═u( "i`)═u( "m`)═u( "e`)═u( " `)═u( "d`)═u( "e`)═u( "c`)═u( "e`)═u( "a`)═u( "s`)═u( "e`)═u( ",`)═u( "
`)═u( "H`)═u( "i`)═u( "s`)═u( " `)═u( "t`)═u( "e`)═u( "n`)═u( "d`)═u( "e`)═u( "r`)═u( " `)═u( "h`)═u( "e`)═u( "i`)═u( "r`)═u( " `)═u( "m`)═u( "i`)═u( "g`)═u( "h`)═u( "t`)═u( " `)═u( "b`)═u( "e`)═u( "a`)═u( "r`)═u( " `)═u( "h`)═u( "i`)═u( "s`)═u( " `)═u( "m`)═u( "e`)═u( "m`)═u( "o`)═u( "r`)═u( "y`)═u( ":`)═u( "
`)═u( "B`)═u( "u`)═u( "t`)═u( " `)═u( "t`)═u( "h`)═u( "o`)═u( "u`)═u( ",`)═u( " `)═u( "c`)═u( "o`)═u( "n`)═u( "t`)═u( "r`)═u( "a`)═u( "c`)═u( "t`)═u( "e`)═u( "d`)═u( " `)═u( "t`)═u( "o`)═u( " `)═u( "t`)═u( "h`)═u( "i`)═u( "n`)═u( "e`)═u( " `)═u( "o`)═u( "w`)═u( "n`)═u( " `)═u( "b`)═u( "r`)═u( "i`)═u( "g`)═u( "h`)═u( "t`)═u( " `)═u( "e`)═u( "y`)═u( "e`)═u( "s`)═u( ",`)═u( "
`)═u( "F`)═u( "e`)═u( "e`)═u( "d`)═u( "'`)═u( "s`)═u( "t`)═u( " `)═u( "t`)═u( "h`)═u( "y`)═u( " `)═u( "l`)═u( "i`)═u( "g`)═u( "h`)═u( "t`)═u( "'`)═u( "s`)═u( " `)═u( "f`)═u( "l`)═u( "a`)═u( "m`)═u( "e`)═u( " `)═u( "w`)═u( "i`)═u( "t`)═u( "h`)═u( " `)═u( "s`)═u( "e`)═u( "l`)═u( "f`)═u( "-`)═u( "s`)═u( "u`)═u( "b`)═u( "s`)═u( "t`)═u( "a`)═u( "n`)═u( "t`)═u( "i`)═u( "a`)═u( "l`)═u( " `)═u( "f`)═u( "u`)
═u( "e`)═u( "l`)═u( ",`)═u( "
`)═u( "M`)═u( "a`)═u( "k`)═u( "i`)═u( "n`)═u( "g`)═u( " `)═u( "a`)═u( " `)═u( "f`)═u( "a`)═u( "m`)═u( "i`)═u( "n`)═u( "e`)═u( " `)═u( "w`)═u( "h`)═u( "e`)═u( "r`)═u( "e`)═u( " `)═u( "a`)═u( "b`)═u( "u`)═u( "n`)═u( "d`)═u( "a`)═u( "n`)═u( "c`)═u( "e`)═u( " `)═u( "l`)═u( "i`)═u( "e`)═u( "s`)═u( ",`)═u( "
`)═u( "T`)═u( "h`)═u( "y`)═u( " `)═u( "s`)═u( "e`)═u( "l`)═u( "f`)═u( " `)═u( "t`)═u( "h`)═u( "y`)═u( " `)═u( "f`)═u( "o`)═u( "e`)═u( ",`)═u( " `)═u( "t`)═u( "o`)═u( " `)═u( "t`)═u( "h`)═u( "y`)═u( " `)═u( "s`)═u( "w`)═u( "e`)═u( "e`)═u( "t`)═u( " `)═u( "s`)═u( "e`)═u( "l`)═u( "f`)═u( " `)═u( "t`)═u( "o`)═u( "o`)═u( " `)═u( "c`)═u( "r`)═u( "u`)═u( "e`)═u( "l`)═u( ".`)═u( "
`)═u( "T`)═u( "h`)═u( "o`)═u( "u`)═u( " `)═u( "t`)═u( "h`)═u( "a`)═u( "t`)═u( " `)═u( "a`)═u( "r`)═u( "t`)═u( " `)═u( "n`)═u( "o`)═u( "w`)═u( " `)═u( "t`)═u( "h`)═u( "e`)═u( " `)═u( "w`)═u( "o`)═u( "r`)═u( "l`)═u( "d`)═u( "'`)═u( "s`)═u( " `)═u( "f`)═u( "r`)═u( "e`)═u( "s`)═u( "h`)═u( " `)═u( "o`)═u( "r`)═u( "n`)═u( "a`)═u( "m`)═u( "e`)═u( "n`)═u( "t`)═u( ",`)═u( "
`)═u( "A`)═u( "n`)═u( "d`)═u( " `)═u( "o`)═u( "n`)═u( "l`)═u( "y`)═u( " `)═u( "h`)═u( "e`)═u( "r`)═u( "a`)═u( "l`)═u( "d`)═u( " `)═u( "t`)═u( "o`)═u( " `)═u( "t`)═u( "h`)═u( "e`)═u( " `)═u( "g`)═u( "a`)═u( "u`)═u( "d`)═u( "y`)═u( " `)═u( "s`)═u( "p`)═u( "r`)═u( "i`)═u( "n`)═u( "g`)═u( ",`)═u( "
`)═u( "W`)═u( "i`)═u( "t`)═u( "h`)═u( "i`)═u( "n`)═u( " `)═u( "t`)═u( "h`)═u( "i`)═u( "n`)═u( "e`)═u( " `)═u( "o`)═u( "w`)═u( "n`)═u( " `)═u( "b`)═u( "u`)═u( "d`)═u( " `)═u( "b`)═u( "u`)═u( "r`)═u( "i`)═u( "e`)═u( "s`)═u( "t`)═u( " `)═u( "t`)═u( "h`)═u( "y`)═u( " `)═u( "c`)═u( "o`)═u( "n`)═u( "t`)═u( "e`)═u( "n`)═u( "t`)═u( ",`)═u( "
`)═u( "A`)═u( "n`)═u( "d`)═u( " `)═u( "t`)═u( "e`)═u( "n`)═u( "d`)═u( "e`)═u( "r`)═u( " `)═u( "c`)═u( "h`)═u( "u`)═u( "r`)═u( "l`)═u( " `)═u( "m`)═u( "a`)═u( "k`)═u( "'`)═u( "s`)═u( "t`)═u( " `)═u( "w`)═u( "a`)═u( "s`)═u( "t`)═u( "e`)═u( " `)═u( "i`)═u( "n`)═u( " `)═u( "n`)═u( "i`)═u( "g`)═u( "g`)═u( "a`)═u( "r`)═u( "d`)═u( "i`)═u( "n`)═u( "g`)═u( ":`)═u( "
`)═u( "P`)═u( "i`)═u( "t`)═u( "y`)═u( " `)═u( "t`)═u( "h`)═u( "e`)═u( " `)═u( "w`)═u( "o`)═u( "r`)═u( "l`)═u( "d`)═u( ",`)═u( " `)═u( "o`)═u( "r`)═u( " `)═u( "e`)═u( "l`)═u( "s`)═u( "e`)═u( " `)═u( "t`)═u( "h`)═u( "i`)═u( "s`)═u( " `)═u( "g`)═u( "l`)═u( "u`)═u( "t`)═u( "t`)═u( "o`)═u( "n`)═u( " `)═u( "b`)═u( "e`)═u( ",`)═u( "
`)═u( "T`)═u( "o`)═u( " `)═u( "e`)═u( "a`)═u( "t`)═u( " `)═u( "t`)═u( "h`)═u( "e`)═u( " `)═u( "w`)═u( "o`)═u( "r`)═u( "l`)═u( "d`)═u( "'`)═u( "s`)═u( " `)═u( "d`)═u( "u`)═u( "e`)═u( ",`)═u( " `)═u( "b`)═u( "y`)═u( " `)═u( "t`)═u( "h`)═u( "e`)═u( " `)═u( "g`)═u( "r`)═u( "a`)═u( "v`)═u( "e`)═u( " `)═u( "a`)═u( "n`)═u( "d`)═u( " `)═u( "t`)═u( "h`)═u( "e`)═u( "e`)═u( ".`)═u( " `)═u( "
Process returned 0 (0x0)   execution time : 0.222 s
Press any key to continue.

i tried to refer other stack overflow discussion like 1, 2, and 3 but still it doesn't solves my problem.

The alphabets are like subjugated in ═u( ")`.. what could be the cause of this problem..any help?


Solution

  • That's because fputs expects null terminated string. You can see the reference here