【问题标题】:How do I stop a blank line from being added in this instance?在这种情况下,如何阻止添加空行?
【发布时间】:2021-08-17 17:44:27
【问题描述】:

我正在尝试这样做,以便您可以编写多行消息,并将其存储在您选择的文件中,然后输入 ` 完成,但是当我在顶部执行此操作时,会打印一个空白行。我怎样才能防止这种情况发生?

#include <stdio.h>
char filename[BUFSIZ];
char input[BUFSIZ];
FILE *file;
int main() {
        printf("What do you want to name the file?\n");
        scanf("%s", filename);
        printf("Enter the contents. Once you are done enter `\n");
        scanf("%[^`]s", input);
        file = fopen(filename, "w");
        fprintf(file, "%s", input);
        fclose(file);
}

【问题讨论】:

  • %[] 转换说明符不会跳过空格,因此它包含前一个 scanf 留下的换行符。在%[] 转换说明符之后也不需要s。要修复它,请删除 s,并在 % 之前放置一个空格。
  • 或者更好的是,使用fgets() 或POSIX getline() 函数一次读取一行,这样您就不必担心缓冲区溢出。
  • 至少在BUFSIZ 上你有8192 字符在Linux 和512 在Windows 上,例如对于 Linux glibc/libio/stdio.h - #define BUFSIZ 8192

标签: c file-io fopen stdio fclose


【解决方案1】:

代码:

/* multi.c
 */

#include <stdio.h>

#define BUFSIZE     1024

int main(void)
{
    char filename[BUFSIZE];
    char input[BUFSIZE];
    FILE *fp;

    /* using fprintf(stderr... is better because of output buffering.
     * This way, we don't need the '\n' at the end for the string to
     * appear in the console. You could also use:
     *
     * printf("Filename: "); fflush(stdout);
     */
    fprintf(stderr, "Filename: ");

    /* When you press ENTER, a '\n' will be created at the stdin buffer.
     * However, this scanf() will NOT read it into @filename. So the '\n'
     * is left in the buffer...
     */
    scanf("%s", filename);

    fprintf(stderr, "Contents: ");
    /* when you call scanf() again, the old '\n' is the first character
     * it reads into @input. So, to discard that, simply add a space
     * before the % - this ignores whitespaces (tabs, newlines, spaces)
     */
    scanf(" %[^`]", input);

    /* open the file, check for errors */
    if(!(fp = fopen(filename, "w"))) {
        perror("fopen");
        return -1;
    }

    /* print the contents to the file */
    fprintf(fp, "%s", input);

    /* clean up */
    fclose(fp);
    return 0;
}

这是执行:

$ ./multi
Filename: hello.txt
Contents: My name is Enzo
I am writing some multiline content to a file
And then I'm posting it to
Stack Overflow.`

这是 hello.txt

$ cat hello.txt
My name is Enzo
I am writing some multiline content to a file
And then I'm posting it to
Stack Overflow.$

请注意,在最后一行之后,我的终端字符 $ 在那里。这是因为我们以 ` 字符结束输入,而不是换行。因此,文件末尾没有换行符。

【讨论】:

    【解决方案2】:
    int main() {
        printf("What do you want to name the file?\n");
        scanf(" %s", filename);
        printf("Enter the contents. Once you are done enter `\n");
        scanf(" %[^`]s", input); //do not forget to put a space in scanf
        file = fopen(filename, "w");
        fprintf(file, " %s", input); //try to make a space in there or if it doesn't work
        fclose(file);                //than try " %[^\n] but this is a little risky depends in the way you want use the code.
                                     //if none of these works than try this other way "\b %s"
    

    }

    始终在“%s”或其他文本格式说明符之前放置一个空格,因为您可能会遇到问题... 原因是当您在 printf() 之后直接添加 scanf() 时,有时 scanf 会将其作为字符串的一部分,它甚至必须读取 printf() 打印的最后一个字符,具体取决于它们是什么类型的字符(主要是空格,\n,\t)。这有点奇怪,但过去发生过很多次。 我认为问题出在那儿

    scanf(" %[^`]s", input);  //put that white space
    

    首先尝试在此处创建该空间,我认为它会起作用,但如果它不尝试留在 cmets 中的其他空间。其中一个肯定会起作用。

    【讨论】:

      猜你喜欢
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多