【问题标题】:Unexpected newline occuring with use of for loops, ternary operators and conditions使用 for 循环、三元运算符和条件时出现意外的换行符
【发布时间】:2016-04-28 15:53:45
【问题描述】:

我了解某些函数会自动将换行符附加到其输出中,但我遇到了不需要换行符的情况。我试图修复它并理解为什么它以多种方式发生,但无济于事。这是我以前没有遇到过的输出问题。

我正在做一个非常基础的项目,来自 K.N. 的 C Programming: A Modern Approach。 King,第 8 章,项目 6:“B1FF 过滤器”,其中某些字符被转换和打印。我使用三元运算符结合条件来获得适当的输出。这是程序:

/*  C Programming: A Modern Approach
        Chapter 8: Arrays, Project 5: B1FF filter
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
    char message[50];
    int msg_len;

    printf("Enter message: ");
    fgets(message, 50, stdin);
    msg_len = strlen(message);

    printf("In B1FF-speak: ");

    for (int i = 0; i < msg_len; i++)
    {
        message[i] = toupper(message[i]);

        if (message[i] == 'A' || message[i] == 'B')
            printf("%c", (message[i] == 'A') ? '4' : '8');

        else if (message[i] == 'E' || message[i] == 'I')
            printf("%c", (message[i] == 'E') ? '3' : '1');

        else if (message[i] == 'O' || message[i] == 'S')
            printf("%c", (message[i] == 'O') ? '0' : '5');

        else
            printf("%c", message[i]);

        if (i == (msg_len - 1))
        {
            for (int j = 0; j < 10; j++)
                printf("!");

            printf("\n");
        }
    }

    return 0;
}

输出不是预期的:

Enter message: C is rilly cool
In B1FF-speak: C 15 R1LLY C00L
!!!!!!!!!!

为什么修改后的字符串和感叹号之间有换行符?我已经明确指定在打印所有文本之后,就在循环终止之前出现换行符。我尝试过更改,例如在循环外打印换行符,不使用 for 循环作为感叹号等,但它会产生相同的结果。它的发生似乎没有明确的原因。

【问题讨论】:

    标签: c output newline


    【解决方案1】:

    fgets 在复制到目标char 缓冲区的内容中包含换行符。因此,您的逐个字符打印例程不会改变它。

    fgets 在 Google 上的结果 #1:http://www.cplusplus.com/reference/cstdio/fgets/

    换行符使fgets停止读取,但它被函数认为是有效字符并包含在复制到str的字符串中。

    当然,正如另一个答案中提到的,您可以在打印时忽略它,或者提前将其设为空。

    【讨论】:

      【解决方案2】:

      fgets() 将读取的换行符保存到缓冲区。如果您不想要它,请将其删除。

      例子:

      fgets(message, 50, stdin);
      /* remove newline character */
      {
          char* lf = strchr(message, '\n');
          if (lf != NULL) *lf = '\0';
      }
      

      意思是:

      fgets(message, 50, stdin); /* read one line from the standard input */
      /* remove newline character */
      {
          char* lf /* declare a variable */
              = strchr(message, '\n'); /* and initialize it with the pointer to the first '\n' in the string message */
          if (lf != NULL) /* check if '\n' is found */
              *lf = '\0'; /* if found, replace '\n' with '\0' and delete the newline character (and string after '\n', which won't usually present) */
      }
      

      【讨论】:

      • @underscore_d 谢谢你的澄清,我一定错过了 fgets() 函数参考中自动附加的换行符。
      • 那么该代码块究竟是如何一步一步完成的呢?顺便说一句,它确实工作得很好,所以谢谢你。我只是想确保在使用它之前了解它的功能。
      • @GabrielSaul 我添加了一些解释。
      • 谢谢,现在才看strchr函数。那么指针的使用可以让变量在找到目标字符时对地址的内容进行操作?
      • @underscore_d 啊,那我可以写一个在打印过程中跳过换行符的条件吗?
      猜你喜欢
      • 1970-01-01
      • 2017-08-21
      • 2017-03-05
      • 2021-07-18
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      相关资源
      最近更新 更多