C语言 删除 C 中的换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2396623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:45:36  来源:igfitidea点击:

Remove new line character in C

c

提问by Biscuit128

I am trying to use getc(character)to take an element from a file and do stuff with it, but it appears that it must have a '\n'before the end of a line is met.

我试图用来getc(character)从文件中获取一个元素并用它做一些事情,但它似乎必须'\n'在遇到行尾之前有一个。

How can I remove this so that when I copy the characters I don't have a new line character appearing anywhere - thus allowing me to deal with printing new lines when I choose?

如何删除它,以便在复制字符时我不会在任何地方出现换行符 - 从而允许我在选择时处理打印新行?

采纳答案by Gabriel ??erbák

Hmm, wouldn't help to use getc to fill a buffer and remove newline and carriage return characters?

嗯,使用 getc 填充缓冲区并删除换行符和回车符无济于事吗?

回答by JUST MY correct OPINION

.
.
.
#include <string.h>
.
. /* insert stuff here */
.
char* mystring = "THIS IS MY STRING\n"
char* deststring;
.
.
.
strncpy(deststring, mystring, strlen(mystring)-1);
.
.
.

(As an added note, I'm not a huge fan of dropping \0 characters in strings like that. It doesn't work well when you start doing i18n and the character width is not fixed. UTF-8, for example, can use anywhere from 1 to 4 bytes per "character".)

(作为补充说明,我不太喜欢在这样的字符串中删除 \0 字符。当您开始执行 i18n 并且字符宽度不固定时,它不能很好地工作。例如,UTF-8 可以每个“字符”使用 1 到 4 个字节。)

回答by Nitinkumar Ambekar

To replace all new line char with spaces, use:

要用空格替换所有新行字符,请使用:

char *pch = strstr(myStr, "\n");
while(pch != NULL)
{
    strncpy(pch, " ", 1);
    pch = strstr(myStr, "\n");
}

To remove first occurrence of new line char in string, use:

要删除字符串中第一次出现的换行符,请使用:

char *pch = strstr(myStr, "\n");
if(pch != NULL)
  strncpy(pch, "
 mystr[ strlen(mystr) - 1 ] = '
buf[strlen(buf)-1] = '
line[strlen(line) - 1] = 0
';
';
", 1);

回答by Justin Ethier

You could replace it with a null terminator.

您可以用空终止符替换它。

Here is one (simple) way to do it off the top of my head:

这是一种(简单)的方法来做到这一点:

char* end = line + strlen(line) - 1 ;          // end of line                                                                                                      
while( (end > line) && isspace(*end) ) end--;  // right trim space                                                                                              
*(end+1) = '##代码##';                               // terminate string

回答by t0mm13b

Supposing that bufis of type charand it holds the string value read in from the file...

假设它buf是类型char并且它保存从文件中读取的字符串值......

##代码##

That sets the second-last character of the buffer to nul i.e. '\0' in order to remove the new-line character.

这将缓冲区的倒数第二个字符设置为 nul,即 '\0' 以删除换行符。

Edit:Since loz mentioned a compiler error I suspect it's a const char *is used...Can we see the code please...

编辑:由于 loz 提到了一个编译器错误,我怀疑它const char *被使用了……我们可以看看代码吗……

回答by James Raitsev

The following will do the trick

以下将解决问题

##代码##

回答by Chris Reid

A bit more complete version:

更完整的版本:

##代码##

(Note: Putting a null char into it makes string readers stop reading at that point but the memory footprint of line is the same. The characters to the right of the '\0' are still there.

(注意:将空字符放入其中会使字符串阅读器在该点停止读取,但行的内存占用量相同。'\0'右侧的字符仍然存在