2

My code is given below. If run this code then even though the text file gets created correctly, for some reason junk values get printed in the console. When I include the string then only the string gets read and printed correctly in the console window and I get junk value for the rest of the variables but when I remove the string completely then I get correct values for rest of the variables. Why is this issue occurring and how to fix it ?

This is the code:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char str[] = "a string";
    char str2[50];
    char ch ='a';
    char ch1;
    int num = 12;
    int num1;
    float deci = 51.15;
    float deci1;
    FILE *new;

    new = fopen("a.txt","w");
    if (new == NULL) {
        printf("Error! file not found! \n");
    }
    fprintf(new, "%s\n", str);
    fprintf(new, "%c\n", ch);
    fprintf(new, "%d\n", num);
    fprintf(new, "%.2f\n", deci);

    fclose(new);

    new = fopen("a.txt", "r");
    if (new == NULL) {
        printf("Error! file not found!  \n");
    }

    fscanf(new, "%[^\n]s", str2);
    //str2[7]='\0';

    fflush(stdin);
    fscanf(new, "%c", &ch1);
    fscanf(new, "%d", &num1);
    fscanf(new, "%f", &deci1);

    //fclose(new);

    printf("string: %s character: %c integer: %d float: %f", str2, ch1, num1, deci1);
    //enter code here
    fclose(new);
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Wasi153
  • 33
  • 6
  • 1
    There is no such [`fscanf`](https://en.cppreference.com/w/c/io/fscanf) format as `%[...]s`. The `s` is *not* part of the format string. Instead it makes `fscanf` attempting to read an *actual* `s` in the input. – Some programmer dude Mar 30 '20 at 09:47
  • 1
    Also note that you don't actually read the newline after the string, it's left and will be read by the `%c` input. Almost *always* use a space before `%c`, as in `" %c"`. – Some programmer dude Mar 30 '20 at 09:48
  • `fscanf(new, "%c", &ch1);` will read the newline that terminated the previous `fscanf`. Change to `fscanf(new, " %c", &ch1);` note the added space. – Weather Vane Mar 30 '20 at 09:48
  • 1
    Note that you did not initialise `ch1` or `num1` or `deci1` so when `fscanf` fails to read, junk values will be printed. – Weather Vane Mar 30 '20 at 09:51
  • 1
    Similary: please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Some explanation: most of the format specifiers for `scanf` automatically filter leading whitespace, but `%c` and `%[...]` and `%n` do not. Adding a space in front of the `%` instructs `scanf` to filter leading whitespace here too. Same for `fscanf`. – Weather Vane Mar 30 '20 at 09:53
  • closing and reopening a file in the same process often causes errors – ralf htp Mar 30 '20 at 09:57
  • Problem Solved ! Thanks to all of you. This problem has been solved. – Wasi153 Mar 30 '20 at 10:02

1 Answers1

0

If I'm not wrong the error is here:

fscanf(new, "%[^\n]s", str2); 

Try to change it with:

fscanf(new, "%[^\n]\n", str2);

This does work for me.

Tommy
  • 21
  • 3
  • Thanks a lot for your help.The problem was already solved. But I will try your suggestion as well – Wasi153 Mar 30 '20 at 10:19
  • `fscanf(new, "%[^\n]\n", str2);` will keep reading the input stream until a non blank character is read or the end of file is reached. This causes unexpected behavior when reading from the console. You should use `fscanf(new, "%[^\n]%*1[\n]", str2);` which will read and discard a single newline at the end of the input line. – chqrlie Mar 30 '20 at 10:32