1

I have been trying to read data from file using fscanf with this format:

fscanf(members, " %[^\n] %[^\n] %d %[^\n] %[^\n] %[^\n]", temp_node->name, temp_node->personal_names, &temp_node->mem_id, temp_node->email, temp_node->boat_class, temp_node->boat_name);

Read function:

member_node read_member(FILE * members){
    int scan = 0;
    member *temp_node;
    temp_node = calloc(1,sizeof(temp_node));

        scan = fscanf(members, " %[^\n] %[^\n] %d %[^\n] %[^\n] %[^\n]", temp_node->name, temp_node->personal_names, &temp_node->mem_id, temp_node->email, temp_node->boat_class, temp_node->boat_name);
        if(scan==EOF){
            free(temp_node);
            return NULL;
        } else{
            return temp_node;
    }
}

Struct def:

typedef struct member{
    char name[20];
    char personal_names[80];
    int mem_id;
    char email[50];
    char boat_class[30];
    char boat_name[30];

}member;

When it starts to read the second line(personal_names) it reads correctly, however it wont stop and starts reading from start in to the same array.

Output in debugger:

name - Morris
personal_names - Huw Rhys MorganMorrisHuw Rhys Morgan 1015... util the array is full
mem_id - 1015
email - h.w.m.morris@\0David\n235\nd.price....
boat_class - noclub.club.uk...
boat_name - Flyer\0f.long...

Expected output:

name - Morris
personal_names - Huw Rhys Morgan
mem_id - 1015
email - h.w.m.morris@nolcub.clubs.uk
boat_class - Flyer
boat_name - The Blue One

The file has this format:

       Morris
Huw Rhys Morgan
1015
h.w.m.morris@noclub.clubs.uk
Flyer
The Blue One
Price
David
235
d.price@noclub.clubs.uk
Invader
Goldcrest
Long
Fred
2
          f.long@long.people.uk
AberSloop
Jaqueta 
  • Succinctly, don't put a blank (tab, newline, etc) at the end of a `fscanf()` format. It means skip white space until the next character that isn't white space. If the input is coming from the terminal, that means the user must guess what character they must type next, which is an appalling UX. – Jonathan Leffler Nov 30 '19 at 22:22
  • If you're adamant that this is not the problem, then you need to provide an MCVE ([Minimal, Complete, Verifiable Example?](https://stackoverflow.com/help/mcve)) (or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). – Jonathan Leffler Nov 30 '19 at 22:25
  • Even after removing the blank spaces it still keeps reading in to the same array from the start of the file or wherever it ended after previous read – Filip Mičánek Nov 30 '19 at 23:01
  • 1
    I didn't say "remove all blank spaces"; only the final one. You've not limited the lengths of the strings — one possibility is a buffer overflow (use `%19[^\n]` to read into a `char name[20];` structure element, for example). At this point, you need to provide the MCVE so we can see what you mean by "reading from the start of the file". Have you captured and checked the return value from `fscanf()`? If it doesn't return 6, then it ran into a parsing error. (Keep the trailing blank out of the format string.) – Jonathan Leffler Nov 30 '19 at 23:04
  • 1
    I edited the question, hopefully you will get a better perspective now – Filip Mičánek Nov 30 '19 at 23:44
  • Also the value from `fscanf()` is returning 6 and the limitations `(%19[^\n])` did not work – Filip Mičánek Nov 30 '19 at 23:48
  • Is a `member_node` a typedef for a pointer to `member`? See [Is it a good idea to typedef pointers](https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) — TL;DR, the answer is normally "No", except perhaps for function pointers. – Jonathan Leffler Dec 01 '19 at 00:48
  • The [comment](https://stackoverflow.com/questions/59120787/fscanf-keeps-reading-from-start?noredirect=1#comment104472182_59120787) by [user3121023](https://stackoverflow.com/users/3121023/user3121023) diagnoses and fixes the problem. When you allocate the size of a pointer instead of the size of a structure, you run into all sorts of undefined behaviour. – Jonathan Leffler Dec 01 '19 at 19:50

0 Answers0