微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

#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);
}

解决方法

代码:

/* 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.$

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

,
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

首先尝试在此处留出空间,我认为它会起作用,但如果它不尝试其他留在评论中的空间。其中之一肯定会起作用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。