C puts() without newline
Asked Answered
P

4

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;
}
Pentateuch answered 21/6, 2013 at 15:17 Comment(1)
Isn't iostream a C++ -only library and using a C++ -only keyword?Juggins
D
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);
Deshabille answered 21/6, 2013 at 15:20 Comment(7)
That "%s" instead of just using "printf(input);" is critical, lest any percent signs in input crash your program.Fabrikoid
calling printf with "%s" is less efficient than fputs, but you probably don't careGallardo
@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.Rootstock
Why not just use the answer below?Electrical
@Rootstock 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.Danelaw
@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 <input file>.Rootstock
@Rootstock but this question is about no newline specifically ;PDanelaw
F
93

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

puts(input);

would become:

fputs(input, stdout);
Fabrikoid answered 21/6, 2013 at 15:21 Comment(7)
You could use printf() as well, but with a small amount of added overhead :-)Fabrikoid
I'm glad I found this answer, as I didn't want to have the overhead of printfSissified
In fact, printf("%s", s) have no overhead over fputs(input, stdout), but fputs looks clean.Hedgepeth
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/…Fabrikoid
@AlexNorth-Keys if you printf with newline, gcc will turn it into puts. godbolt.org/z/784dEvnjPTelesis
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.Fabrikoid
But it won't when using printf("%s", ...) without a newline, which was the original question: godbolt.org/z/h4jWeobY6 So fputs(s, stdout) really is the right answer here over printf("%s", s).Tubercle
D
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);
Deshabille answered 21/6, 2013 at 15:20 Comment(7)
That "%s" instead of just using "printf(input);" is critical, lest any percent signs in input crash your program.Fabrikoid
calling printf with "%s" is less efficient than fputs, but you probably don't careGallardo
@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.Rootstock
Why not just use the answer below?Electrical
@Rootstock 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.Danelaw
@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 <input file>.Rootstock
@Rootstock but this question is about no newline specifically ;PDanelaw
Q
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
Quotable answered 18/12, 2019 at 21:10 Comment(0)
J
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;.

Juggins answered 29/9, 2022 at 20:48 Comment(1)
It works but could be quite inefficient if stdout is not buffered.Kunin

© 2022 - 2024 — McMap. All rights reserved.