Hi. I have a troubble with reading string from file and saving it in a structure.

Here's my code:

FILE*t;
    t=fopen("file.txt", "r");
    wt=(pointer)malloc(sizeof(list));

    w=wt;

    for(i=0; i<3; ++i)    
    {   
        w=(pointer)malloc(sizeof(list));
        fscanf(t, "%d", &w->id_t);
        fscanf(t, "%s", &w->name);
        fscanf(t, "%f", &w->price);
        fscanf(t, "%d", &w->tax);
        printf("%d\n", w->name); //for check
        w->next=NULL;
        fscanf(t, "%c", &c);
        w=w->next;
    }

The only problem is with

fscanf(t, "%s", &w->name);

What have I to do?

So what's the value of w->name after you read it?

When asking a question, give us ALL the details necessary to understand the problem. For example, how do you know that statement is the problem?

Sorry.
So my file looks like:

1 tomato 0.34 22
2 carrot 0.30 22

When I'm reading a file every variables values are ok except name. Application sends me some numbers during name reading, over 1 milion like 6381943.

For that matter, think about the type of w which was returned by a call to malloc() Why are you taking its address?

and: what is pointer which is being used to cast the void * returned by malloc()

struct S_list
{
  int id_t;
  char *name;
  float price;
  int tax;
  int stat;
  struct S_list*next;
};
typedef struct S_list list;
typedef list *pointer;

This is my structure. I don't understand what is wrong with my malloc function.

I don't understand what is wrong with my malloc function.

Nothing. I was looking at your fscanf calls where you pass &w->name (for instance). I had forgotten that -> binds tighter than & so it looked to me as if you were taking the address of the address of w. It might be smart, as 'self documenting code' to put parens so as to make it easy for even cross-eyed people such as myself to read it correctly: &(w->whatever)

Take a look at your original line 5 and then line 9... and then line 17. Is that what you really intend? I think line 9 is wrong, and may be your bug.

I have been trying to read string from file and saving it in that structure in many ways. If I delete line 9, it won't do anything else with that what is in the loop. I also made some variables to fscanf to them and then to structure. But every time I tried to read w->name some numbers appears.

If you don't delete line 9, then lines 5 (and 3) and 17 can be removed, since line 9 writes over the top of the w that you set in those lines. Think about this:

  • Where (what line) is the head of the list malloc'd?
  • Where is the next link (anything after the head) malloc'd?

You really want each of those things to happen in exactly one place.

Line 17 is the next link. If I point back on the head (wt) and start to printf every structure's variable only name doesn't match.

Your problem is the variable you defined to hold the string is just a pointer. There is no space to actually store the value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.