C 如何阻止在此实例中添加空行?

C 如何阻止在此实例中添加空行?,c,file-io,fopen,stdio,fclose,C,File Io,Fopen,Stdio,Fclose,我正在尝试这样做,以便您可以编写多行消息,并将其存储在您选择的文件中。要完成此操作,请输入`但当我在顶部执行此操作时,会打印一个空行。我怎样才能防止这种情况 #include <stdio.h> char filename[BUFSIZ]; char input[BUFSIZ]; FILE *file; int main() { printf("What do you want to name the file?\n"); scan

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

#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);
}
#包括
字符文件名[BUFSIZ];
字符输入[BUFSIZ];
文件*文件;
int main(){
printf(“您想将文件命名为什么?\n”);
scanf(“%s”,文件名);
printf(“输入内容。完成后输入“\n”);
扫描频率(“%[^`]s”,输入);
file=fopen(文件名,“w”);
fprintf(文件“%s”,输入);
fclose(文件);
}
}

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

scanf(" %[^`]s", input);  //put that white space
首先试着在这里留出空间,我认为它会起作用,但如果它不尝试其他留下的评论。其中一个肯定会起作用。

代码:

/* 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;
}
这是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.$

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

转换说明符
%[]
不会跳过空格,因此它包含上一个
scanf
留下的换行符。在
%[]
转换说明符之后也不需要
s
。若要修复它,请删除
s
,并在
%
之前留出一个空格。或者更好的做法是,使用
fgets()
或POSIX
getline()
函数一次读取一行,这样您就不必担心缓冲区溢出。至少使用
BUFSIZ
在Linux上有
8192
字符,在windows上有
512
。,例如,对于Linux
$ ./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.`
$ 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.$