Reputation: 783
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;
}
Upvotes: 45
Views: 45318
Reputation: 148
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;
.
Upvotes: 0
Reputation: 840
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
Upvotes: 5
Reputation: 4363
Typically one would use fputs() instead of puts() to omit the newline. In your code, the
puts(input);
would become:
fputs(input, stdout);
Upvotes: 95
Reputation: 726509
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);
Upvotes: 16