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;
}
1
  • 1
    Isn't iostream a C++ -only library and using a C++ -only keyword?
    – markoj
    Commented Oct 1, 2022 at 21:53

4 Answers 4

93

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

puts(input);

would become:

fputs(input, stdout);
7
  • 3
    You could use printf() as well, but with a small amount of added overhead :-) Commented Jun 21, 2013 at 15:22
  • 13
    I'm glad I found this answer, as I didn't want to have the overhead of printf Commented Jan 13, 2018 at 20:38
  • In fact, printf("%s", s) have no overhead over fputs(input, stdout), but fputs looks clean.
    – Nick
    Commented Aug 27, 2020 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. code.woboq.org/userspace/glibc/stdio-common/… Commented Aug 28, 2020 at 16:33
  • 1
    @AlexNorth-Keys if you printf with newline, gcc will turn it into puts. godbolt.org/z/784dEvnjP
    – qwr
    Commented Sep 12, 2022 at 18:27
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);
7
  • 11
    That "%s" instead of just using "printf(input);" is critical, lest any percent signs in input crash your program. Commented Jun 21, 2013 at 15:23
  • 8
    calling printf with "%s" is less efficient than fputs, but you probably don't care
    – benathon
    Commented Feb 5, 2014 at 0: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. Commented Sep 6, 2016 at 11:45
  • Why not just use the answer below? Commented May 12, 2019 at 6: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.
    Commented Jun 30, 2021 at 13:25
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
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;.

1
  • 1
    It works but could be quite inefficient if stdout is not buffered. Commented Jul 24, 2023 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.