Printf ("something \ n") outputs "something" (extra space) (g ++ / linux / read output file with gedit)
I have a simple C ++ program that reads stdin
with scanf
and returns results stdout
with printf
:
#include <iostream>
using namespace std;
int main()
{
int n, x;
int f=0, s=0, t=0;
scanf("%d",&n); scanf("%d",&x);
for(int index=0; index<n; index++)
{
scanf("%d",&f);
scanf("%d",&s);
scanf("%d",&t);
if(x < f)
{
printf("first\n");
}
else if(x<s)
{
printf("second\n");
}
else if(x<t)
{
printf("third\n");
}
else
{
printf("empty\n");
}
}
return 0;
}
I am compiling with g ++ and running under Linux. I run the program using a text file as input, and wrap the output out to another text file like this:
in.txt> out.txt
The problem is that out.txt looks like this:
result1_
result2_
result3_
...
Where "_" is extra space at the end of each line. I am browsing out.txt in gedit.
How can I create an output without extra space?
My input file looks like this:
2 123
123 123 123
123 234 212
Edit: I was able to find a workaround for this problem: printf("\rfoo");
Thanks for your input!
source to share
End of line characters:
System Hex Value Type
Mac 0D 13 CR
DOS 0D 0A 13 10 CR LF
Unix 0A 10 LF
For line endings on each system, you can:
printf("%c", 13);
printf("%c%c", 13, 10);
printf("%c", 10);
You can use this like
printf("empty");
printf("%c", 10);
Wikipedia New article here.
source to share
Try removing '\ n' from statements printf()
and run the code again. If the output file looks like one long word (no spaces), then you know that the only thing inserted after the text is "\ n".
I am guessing that the editor you are using to read the out.txt file is just making it look like extra space after exiting.
If you are still not sure, you can write a quick program to read out.txt and determine the ASCII code of each character.
source to share
Ok, this is a little tricky to understand as the example program has a lot of errors:
g++ -o example example.cc
example.cc: In function 'int main()':
example.cc:19: error: 'k' was not declared in this scope
example.cc:22: error: 'o' was not declared in this scope
example.cc:24: error: 'd' was not declared in this scope
make: *** [example] Error 1
But that won't be your input file; your scanf will download whatever you put in int
s. This example though:
/* scan -- try scanf */
#include <stdio.h>
int main(){
int n ;
(void) scanf("%d",&n);
printf("%d\n", n);
return 0;
}
produced this result:
bash $ ./scan | od -c
42
0000000 4 2 \n
0000003
on Mac OS / X. Get a copy of the code you are actually running and the results of od -c.
source to share
First, the code sample you provided does not compile since o and d are undefined ...
Secondly, you probably got a space at the end of the line you are reading from the input file. Try opening it in vi to see. Otherwise you can call the trimmer function on each line before exiting and be done with it.
Good luck!
source to share
It looks to me like it's not even close to it, but if you run this on Windows, you get \ r \ n as line terminators and maybe under * nix, in a non-Windows text editor, you get \ r as total white space as \ r cannot be printed.
Long shot, the best way to test this is to use a hex editor and see the file yourself.
source to share