c 删除新行字符帮助

发布于 2024-10-07 17:52:33 字数 373 浏览 0 评论 0原文

我对 c 很陌生,如果这是一个愚蠢的问题,我很抱歉!

我有这个:

  fgets(question,200,stdin);
  char *memq = malloc(sizeof(strlen(question)));
  memq= question;

但是问题变量的末尾总是有一个新行! 我该如何删除它/防止它发生?

我试过了:

  fgets(question,200,stdin);
  char *memq = malloc(sizeof(strlen(question))-sizeof(char));
  memq= question;

没有效果!

Im fairly new to c sorry if this is a stupid question!

i have this:

  fgets(question,200,stdin);
  char *memq = malloc(sizeof(strlen(question)));
  memq= question;

however the question variable always has a new line on the end of it !
how do i remove this / prevent it happening?

i have tried this:

  fgets(question,200,stdin);
  char *memq = malloc(sizeof(strlen(question))-sizeof(char));
  memq= question;

there was no effect!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

ゞ花落谁相伴 2024-10-14 17:52:33

要摆脱换行符,在 malloc 之前,执行 :

question[strlen(question)-1] = 0;

另一种选择(在下面的评论中建议)将执行以下操作:

question[strcspn(question, "\n")] = '\0';

将 malloc 行更改为:

char *memq = malloc(strlen(question)+1);

将分配行更改

memq = question;

为:

strcpy (memq, question);

to get rid of the newline, before your malloc, do :

question[strlen(question)-1] = 0;

an alternative (suggested in the comments below) would be to do the following instead:

question[strcspn(question, "\n")] = '\0';

change the malloc line to:

char *memq = malloc(strlen(question)+1);

change the assignation line:

memq = question;

to:

strcpy (memq, question);
请你别敷衍 2024-10-14 17:52:33

这段代码被严重破坏了。

如果您打算将数据复制到 memq 中,则需要在其中分配 strlen+1 字节(为什么要执行 sizeof?这将分配 4 个字节)字节,因为 sizeof(strlen())sizeof(int))。

您不能只是将 question 分配给 memq 并期望将数据复制进去。这一切都会覆盖您刚刚 malloc 编入的指针memq,泄漏它。你必须这样做

strcpy(memq, question);

这就是为什么你需要在memq中额外的字节,因为这包括一个空终止符。此时,您可以从 memq 中删除换行符,如其他地方所述。

This code is badly broken.

You need to allocate strlen+1 bytes into memq if you plan to copy the data there (why are you doing sizeof? that will allocate 4 bytes since sizeof(strlen()) is sizeof(int)).

You cannot just assign question to memq and expect the data to be copied in. All this does is overwrite the pointer you just malloc-ed into memq, leaking it. You have to do

strcpy(memq, question);

That's why you need the extra byte in memq, since this includes a null terminator. At this point you are in position to remove the newline from memq as noted elsewhere.

辞取 2024-10-14 17:52:33

假设您的输入是 ABCDEn,其中 n 代表新行。

您将读取 ABCDEn0,其中 0 代表 null,它终止字符串。

因此,通过去掉最后一个字符,您将去掉空值,而不是换行符。我会像你一样去掉最后一个字符,但然后将(新的)最后一个字符设置为 null 以终止你的字符串。

Let's say your input is ABCDEn where the n represents the new line.

You're going to be reading in ABCDEn0 where 0 represents null, which terminates the string.

So, by taking off the last char, you're taking off the null, not the newline. I would take off the last char as you are, but then set the (new) last char to null to terminate your string.

南城追梦 2024-10-14 17:52:33

在这两种情况下,memq = Question 都是错误。您刚刚丢失了指向新分配的空间的指针,而是将 question 的第一个字符的地址复制到 memq

使用 strcpy或者用 memcpy 来代替,将 question 指向的字符串的内容复制到 memq< 指向的内存内容/代码>。

In both cases, memq = question is wrong. You've just lost your pointer to new newly-allocated space, and instead copied the address of the first character of question to memq

Use strcpy or memcpy instead, to copy the contents of the string pointed to by question to the contents of the memory pointed to by memq.

慕烟庭风 2024-10-14 17:52:33

暗示:
删除最后一个字符! (提示提示:你知道长度)

Hint:
Remove the last character! (Hint to hint: you know the length)

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