How do I stop a blank line from being added in this instance?

159 Views Asked by At

I am trying to make it so you can write a multi line message and it will store it in a file of your choosing and to be done you enter ` but when I do this at the top there is a blank line printed. How can I prevent this?

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

There are 2 best solutions below

0
ALB_DEV On
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"

}

always put a white space before " %s" or other text format specificators because, you may have problems... The reason is that when you add a scanf() directely after a printf() sometimes it scanf takes as part of the string that it has to read even the last character printed by printf(), depending in what kind of characters they are (mostly white spaces, \n, \t). It's a little bit weird but it happened me a lot of times in the past. I think that the problem is in there

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

first try to make that space here and I think it will work, but if it doesn't try the others left in comments. One of them will definitively work.

0
Enzo Ferber On

Code:

/* 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;
}

This is the execution:

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

And this is 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.$

Notice that after the last line my terminal character $ is there. This is because we end the input with a ` character, and not with a new line. Therefore, there's no new-line at the end of the file.