C puts() without newline

Constantine picture Constantine · Jun 21, 2013 · Viewed 31.1k times · Source

I currently have this program that prints a text file on the console, but every line has an extra new line below it. if the text was

hello world

it would output hello

world

the code is this

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    FILE* fp;
    char input[80], ch = 'a';
    char key[] = "exit\n";
    int q;

    fp = fopen("c:\\users\\kostas\\desktop\\original.txt", "r+");

    while (!feof(fp)) {
        fgets(input, 80, fp);
        puts(input);
    }
    fclose(fp);

    return 0;
}

Answer

Alex North-Keys picture Alex North-Keys · Jun 21, 2013

Typically one would use fputs() instead of puts() to omit the newline. In your code, the

puts(input);

would become:

fputs(input, stdout);