44

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;
}
Al.G.
  • 4,327
  • 6
  • 31
  • 56
Constantine
  • 763
  • 2
  • 9
  • 19

4 Answers4

92

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

puts(input);

would become:

fputs(input, stdout);
Alex North-Keys
  • 4,200
  • 1
  • 20
  • 22
  • 3
    You could use printf() as well, but with a small amount of added overhead :-) – Alex North-Keys Jun 21 '13 at 15:22
  • 13
    I'm glad I found this answer, as I didn't want to have the overhead of `printf` – David Callanan Jan 13 '18 at 20:38
  • In fact, `printf("%s", s)` have no overhead over `fputs(input, stdout)`, but `fputs` looks clean. – Nick Aug 27 '20 at 15:26
  • 3
    The `printf("%s", s)` has to parse the format string, which is definitely less overhead than a system call - but have you *seen* the code for parsing the format string? That's definitely overhead. q.v. https://code.woboq.org/userspace/glibc/stdio-common/vfprintf-internal.c.html – Alex North-Keys Aug 28 '20 at 16:33
  • @AlexNorth-Keys if you printf with newline, gcc will turn it into puts. https://godbolt.org/z/784dEvnjP – qwr Sep 12 '22 at 18:27
  • Well, wait long enough and gcc might turn it into some more efficient quantum computed thing. Compiler magic marches on, although this often impedes debugging. – Alex North-Keys Sep 13 '22 at 19:29
  • But it won't when using `printf("%s", ...)` without a newline, which was the original question: https://godbolt.org/z/h4jWeobY6 So `fputs(s, stdout)` really is the right answer here over `printf("%s", s)`. – Nicolas Noble Jul 09 '23 at 18:23
16

puts() adds the newline character by the library specification. You can use printf instead, where you can control what gets printed with a format string:

printf("%s", input);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 11
    That "%s" instead of just using "printf(input);" is critical, lest any percent signs in input crash your program. – Alex North-Keys Jun 21 '13 at 15:23
  • 7
    calling printf with "%s" is less efficient than fputs, but you probably don't care – benathon Feb 05 '14 at 00:46
  • 2
    @portforwardpodcast Compiler do change printf("%s",sting) to fputs(string,stdout), when you enalbe optimization, at least gcc does this. Some compiler maybe change it to fwrite() which is faster when you know the string lenght. – 12431234123412341234123 Sep 06 '16 at 11:45
  • Why not just use the answer below? – DexterHaxxor May 12 '19 at 06:31
  • @12431234123412341234123 when did it do that? I don't see this transformation being done with any version of GCC I have available. I think the main issue is getting the stdout at the point of optimization. – Dan M. Jun 30 '21 at 13:25
  • @DanM. You are correct, my example was wrong, but in case when you have `"%s\n"` as format string gcc will replace it with `puts()`. Try to compile `main(){printf("%s\n","");}` with `gcc `. – 12431234123412341234123 Jul 01 '21 at 16:10
  • @12431234123412341234123 but this question is about no newline specifically ;P – Dan M. Jul 01 '21 at 16:26
5

You can also write a custom puts function:

#include <stdio.h>

int my_puts(char const s[static 1]) {
    for (size_t i = 0; s[i]; ++i)
        if (putchar(s[i]) == EOF) return EOF;

    return 0;
}

int main() {
    my_puts("testing ");
    my_puts("C puts() without ");
    my_puts("newline");

    return 0;
}

Output:

testing C puts() without newline
Ferrarezi
  • 801
  • 10
  • 12
0

This should work:

#include<stdio.h>
void put_s(char* s){
    while(*s) putchar(*s++);
}

Just for the sakes of having more examples, here is another one involving recursion:

#include<stdio.h>
void put_s(char* s){
    if(!*s) return;
    putchar(*s);
    put_s(s+1);
}

Note: I noticed that your code wouldn't compile, because of the #include<iostream> and the using namespace std;.

markoj
  • 150
  • 10