printf(“something\n”) 输出“something” (额外的空间)(g++/linux/使用 gedit 读取输出文件)

发布于 2024-07-10 03:39:46 字数 1248 浏览 11 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

瞄了个咪的 2024-07-17 03:39:46

尝试从 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.

草莓味的萝莉 2024-07-17 03:39:46

行尾字符为:

System  Hex     Value   Type
Mac     0D      13      CR
DOS     0D 0A   13 10   CR LF
Unix    0A      10      LF 

对于每个系统上的行尾,您可以:

printf("%c", 13);
printf("%c%c", 13, 10);
printf("%c", 10);

一样使用它

printf("empty");
printf("%c", 10);

您可以像Wikipedia Newline 文章

The end of line chars are:

System  Hex     Value   Type
Mac     0D      13      CR
DOS     0D 0A   13 10   CR LF
Unix    0A      10      LF 

For a end of line 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 Newline article here.

瞳孔里扚悲伤 2024-07-17 03:39:46

好吧,弄清楚这一点有点困难,因为示例程序有很多错误:

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

但它不会是您的输入文件;它不会是您的输入文件。 你的 scanf 将加载你在 int 中输入的任何内容。 不过,这个例子:

/* scan -- try scanf */
#include <stdio.h>

int main(){
    int n ;
    (void) scanf("%d",&n);
    printf("%d\n", n);
    return 0;
}

产生了这个结果:

bash $ ./scan | od -c
42
0000000    4   2  \n                                                    
0000003

在 Mac OS/X 上。 向我们提供您实际运行的代码的副本以及 od -c 的结果。

Okay, it's a little hard to figure this out, as the example program has numerous 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 it's not going to be your input file; your scanf will be loading whatever you're typing into ints. 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 us a copy of the code you're actually running, and the results of od -c.

夜灵血窟げ 2024-07-17 03:39:46

这里需要更多信息,正如 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?

童话 2024-07-17 03:39:46

我的猜测是你的空间并不是真正的空间。 跑去

od -hc out.txt

仔细检查它是否确实是一个空格。

My guess is that your space isn't really a space. Run

od -hc out.txt

to double check that it is really a space.

影子是时光的心 2024-07-17 03:39:46

首先,您给出的代码示例无法编译,因为 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!

挥剑断情 2024-07-17 03:39:46

确保您正在查看您期望的程序的输出; 这有一个语法错误(int n 之后没有“;”)。

Make sure you're looking at the output of the program you expect; this has a syntax error (no ";" after int n).

左秋 2024-07-17 03:39:46

我觉得它还没有接近这个,但是如果你在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文