-1

I am trying to read a file, assign the values to the structure members and then add the values into a dynamic memory allocation. I used a for loop to read all the values and then add them to the dynamic array. It gives me errors while I compile it.

How could I assign the values to the Structure members and at the same time add them to a dynamic array?

I would really appreciate your assistance !! thanks.

Here is my code

struct Student
{
    char *name[20];       
    char *gender[10];     
    int   age;           
    char *course[20];       
};

struct elements 
{
    struct Student *s;
    struct elements *next;
};

FILE *file;
char c;
char type[30];

int main(int args, char *arg[])
{
    struct Student *school = (struct Student *)malloc(sizeof(struct Student)); // Dynamic array

    file = fopen(arg[1], "r");  // reading file

    if(file != NULL)
    {
        while((c = fgetc(file)) != EOF) putchar(c);
        {
            int size;
            int r;
            for (r=0; r<size;r++)
            {
                fscanf("Name: %s", school->name);

                //school->name = Addstudent(); // didn't work
                //printf("names are: %s\n",school->name); // here I want to make sure the value name is in the array
            }
         }
    }
    else 
    {
        printf("Unable to open file \n");   
    }
    free(file);
    return 0;
}

char* Addstudent()
{
    char* name;
    fscanf(file, "Name: %s",type ,name);
    return name;
}

Here are the Errors

In function ‘main’: Latest1.c:59:4: warning: passing argument 1 of ‘fscanf’ from incompatible pointer type [enabled by default] /usr/include/stdio.h:449:12: note: expected ‘struct FILE * restrict’ but argument is of type ‘char *’

Latest1.c:59:4: warning: passing argument 2 of ‘fscanf’ from incompatible pointer type [enabled by default] /usr/include/stdio.h:449:12: note: expected ‘const char * restrict’ but argument is of type ‘char **’

Latest1.c:59:4: warning: format not a string literal and no format arguments [-Wformat-security]

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
HShbib
  • 1,811
  • 3
  • 25
  • 47

2 Answers2

1

For starters, this code is probably not doing what you want it to do:

while((c = fgetc(file)) != EOF) putchar(c);
{
  ...
}

By fixing the formatting, you can see that it's evaluated as

while((c = fgetc(file)) != EOF)
  putchar(c);

{
  ...
}

The while loop contains only putchar(c), and the code within the braces will be executed only once, after reaching EOF.

It's also usually a bad idea to mix calls to fgetc() and fscanf(). It's much safer to read one line at a time into a variable, and then parse the data from the variable:

char buf[MAX_LEN];
while (fgets(buf, MAX_LEN, file) != NULL) {
  sscanf(buf, "%s", school->name);
}

When you're done reading the file, you need to fclose it, not free it.

Finally, although you've allocated memory for the elements in the Student structure, you haven't allocated space for the data that the name, gender, and course pointers will point to.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
0

so many errors with your code;

  1. do you really understand what this declares?
    char *name[20]

  2. really look closely at this line, this neither an array or what you apparently want to do: struct Student *school = (struct Student *)malloc(sizeof(struct Student)); // Dynamic array

  3. read the documentation on fscanf

(ps sorry if formatting is off, still getting to grips with new phone)

Nim
  • 33,299
  • 2
  • 62
  • 101