printf(“something \ n”)输出“something”(附加空格)(g ++ / linux /用gedit读取输出文件)

时间:2022-08-01 21:19:53

I have a simple C++ program that reads stdin using scanf and returns results to stdout using printf:

我有一个简单的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;
}

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:

我正在用g ++编译并在linux下运行。我使用文本文件作为输入执行程序,并将输出传递给另一个文本文件,如下所示:

program < in.txt > out.txt

program out.txt

The problem is that out.txt looks like this:

问题是out.txt看起来像这样:

result1_
result2_
result3_
...

result1_ result2_ result3_ ...

Where '_' is an extra space at the end of each line. I am viewing out.txt in gedit.

其中'_'是每行末尾的额外空格。我在gedit中查看out.txt。

How can I produce output without the additional space?

如何在没有额外空间的情况下生成输出?

My input file looks like this:

我的输入文件如下所示:

2 123
123 123 123
123 234 212

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!

编辑:我能找到解决此问题的方法:printf(“\ rfoo”);感谢您的输入!

8 个解决方案

#1


2  

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'.

尝试从printf()语句中删除'\ n',然后再次运行代码。如果输出文件看起来像一个长字(没有空格),那么你知道在文本之后插入的唯一东西就是'\ 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.

我假设你用来读取out.txt文件的编辑器使它看起来像输出后有一个额外的空格。

If you are still unsure, you can write a quick program to read in out.txt and determine the ASCII code of each character.

如果您仍然不确定,可以编写一个快速程序来读取out.txt并确定每个字符的ASCII代码。

#2


2  

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.

Wikipedia Newline文章在这里。

#3


1  

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:

但它不会是你的输入文件;你的scanf将加载你正在输入整数的内容。但是这个例子:

/* 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.

在Mac OS / X上。获取您实际运行的代码的副本以及od -c的结果。

#4


0  

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?

正如timhon所问,这里需要更多的信息,你在哪个环境下工作? Linux,Windows,Mac?另外,您使用哪种文本编辑器显示这些额外的空格?

#5


0  

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.

仔细检查它是否真的是一个空间。

#6


0  

First, the code sample you've given doesn't compile as o and d are not defined...

首先,您给出的代码示例不会编译为o和d未定义...

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.

其次,您可能在输入文件中读取的行末尾有空格。尝试在vi中打开它来查看。否则,您可以在输出之前调用每行上的trim函数并完成它。

Good luck!

#7


0  

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

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

#8


0  

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.

我觉得它甚至不接近这个,但是如果你在Windows上运行它,你会得到\ r \ n作为行终止符,并且,或许,在* nix下,在非Windows知识的文本编辑器下,你'将\ r作为公共空格,因为\ r \ n不可打印。

Long shot, the best way to test this is using an hexadecimal editor and see the file yourself.

远射,测试这个的最好方法是使用十六进制编辑器并自己查看文件。

#1


2  

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'.

尝试从printf()语句中删除'\ n',然后再次运行代码。如果输出文件看起来像一个长字(没有空格),那么你知道在文本之后插入的唯一东西就是'\ 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.

我假设你用来读取out.txt文件的编辑器使它看起来像输出后有一个额外的空格。

If you are still unsure, you can write a quick program to read in out.txt and determine the ASCII code of each character.

如果您仍然不确定,可以编写一个快速程序来读取out.txt并确定每个字符的ASCII代码。

#2


2  

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.

Wikipedia Newline文章在这里。

#3


1  

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:

但它不会是你的输入文件;你的scanf将加载你正在输入整数的内容。但是这个例子:

/* 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.

在Mac OS / X上。获取您实际运行的代码的副本以及od -c的结果。

#4


0  

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?

正如timhon所问,这里需要更多的信息,你在哪个环境下工作? Linux,Windows,Mac?另外,您使用哪种文本编辑器显示这些额外的空格?

#5


0  

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.

仔细检查它是否真的是一个空间。

#6


0  

First, the code sample you've given doesn't compile as o and d are not defined...

首先,您给出的代码示例不会编译为o和d未定义...

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.

其次,您可能在输入文件中读取的行末尾有空格。尝试在vi中打开它来查看。否则,您可以在输出之前调用每行上的trim函数并完成它。

Good luck!

#7


0  

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

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

#8


0  

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.

我觉得它甚至不接近这个,但是如果你在Windows上运行它,你会得到\ r \ n作为行终止符,并且,或许,在* nix下,在非Windows知识的文本编辑器下,你'将\ r作为公共空格,因为\ r \ n不可打印。

Long shot, the best way to test this is using an hexadecimal editor and see the file yourself.

远射,测试这个的最好方法是使用十六进制编辑器并自己查看文件。