printf(“something\n”) 输出“something” (额外的空间)(g++/linux/使用 gedit 读取输出文件)
我有一个简单的 C++ 程序,它使用 scanf
读取 stdin
并使用 printf
将结果返回到 stdout
:
#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;
}
我正在编译使用g++并在linux下运行。 我使用文本文件作为输入来执行程序,并将输出通过管道传输到另一个文本文件,如下所示:
程序< 在.txt> 输出.txt
问题是 out.txt 看起来像这样:
结果1_
结果2_
结果3_
...
其中“_”是每行末尾的额外空格。 我正在 gedit 中查看 out.txt。
如何在没有额外空间的情况下生成输出?
我的输入文件如下所示:
2 123
123 123 123
123234212
编辑:我能够找到此问题的解决方法:printf("\rfoo");
感谢您的输入!
I have a simple C++ program that reads stdin
using scanf
and returns results to stdout
using 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 execute the program using a text file as input, and pipe the output to another text file as follows:
program < in.txt > out.txt
The problem is that out.txt looks like this:
result1_
result2_
result3_
...
Where '_' is an extra space at the end of each line. I am viewing out.txt in gedit.
How can I produce output without the additional 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 issue: printf("\rfoo");
Thanks for your input!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试从
printf()
语句中删除“\n”,然后再次运行代码。 如果输出文件看起来像一个长单词(没有空格),那么您就知道在文本后面插入的唯一内容就是“\n”。我假设您用来读取 out.txt 文件的编辑器只是让它看起来输出后有一个额外的空格。
如果您仍然不确定,可以编写一个快速程序来读入 out.txt 并确定每个字符的 ASCII 代码。
Try removing the '\n' from your
printf()
statements, and run the code again. If the output file looks like one long word (no spaces), then you know that the only thing being inserted after the text is that '\n'.I assume that the editor you are using to read the out.txt file just makes it look like there is an extra space after the output.
If you are still unsure, you can write a quick program to read in out.txt and determine the ASCII code of each character.
行尾字符为:
对于每个系统上的行尾,您可以:
一样使用它
您可以像Wikipedia Newline 文章
The end of line chars are:
For a end of line on each system you can:
You can use this like
Wikipedia Newline article here.
好吧,弄清楚这一点有点困难,因为示例程序有很多错误:
但它不会是您的输入文件;它不会是您的输入文件。 你的 scanf 将加载你在
int
中输入的任何内容。 不过,这个例子:产生了这个结果:
在 Mac OS/X 上。 向我们提供您实际运行的代码的副本以及 od -c 的结果。
Okay, it's a little hard to figure this out, as the example program has numerous errors:
But it's not going to be your input file; your scanf will be loading whatever you're typing into
int
s. This example, though:produced this result:
on Mac OS/X. Get us a copy of the code you're actually running, and the results of od -c.
这里需要更多信息,正如 Timhon 所问的,您在哪种环境下工作? Linux、Windows、Mac? 另外,您使用什么文本编辑器来显示这些额外的空格?
More information is needed here, as timhon asked, which environment are you working under? Linux, Windows, Mac? Also, what text editor are you using which displays these extra spaces?
我的猜测是你的空间并不是真正的空间。 跑去
仔细检查它是否确实是一个空格。
My guess is that your space isn't really a space. Run
to double check that it is really a space.
首先,您给出的代码示例无法编译,因为 o 和 d 未定义...
其次,您从输入文件读取的行末尾可能有空格。 尝试用vi打开看看。 否则,您可以在输出之前在每行上调用修剪函数并完成它。
祝你好运!
First, the code sample you've given doesn't compile as o and d are not defined...
Second, you've probably got whitespace at the end of the line you're reading in from the input file. Try opening it in vi to see. Otherwise, you can call a trim function on each line prior to output and be done with it.
Good luck!
确保您正在查看您期望的程序的输出; 这有一个语法错误(
int n
之后没有“;”)。Make sure you're looking at the output of the program you expect; this has a syntax error (no ";" after
int n
).我觉得它还没有接近这个,但是如果你在 Windows 上运行它,你会得到 \r\n 作为行终止符,并且,也许,在 *nix 下,在非 Windows 感知的文本编辑器下,你'将得到 \r 作为公共空格,因为 \r 不可打印。
从长远来看,测试这一点的最佳方法是使用十六进制编辑器并亲自查看该文件。
I feel like it's not even close to this, but if you run this on Windows, you'll get \r\n as line terminators, and, maybe, under *nix, under a non-Windows-aware text editor, you'll get \r as a common blank space, since \r is not printable.
Long shot, the best way to test this is using an hexadecimal editor and see the file yourself.