2

Sample Text file:

234765 PETER 
867574 SMITH 

I'm trying to take the id and string from the text file and save it into a struct. The id is saving fine but the string isn't.

typedef struct student 
{
    int id[DATA_SIZE];
    char *student[DATA_SIZE];
}studentinfo;
studentinfo list;

struct student *create_space(int size)
{
    struct student *tmp = (struct student*)malloc(size*sizeof(struct student));
    return(tmp);
}
struct student * readData(struct student*pointer,studentinfo v)
{
    int count =0;
    int tmpid;
    char str[256];
    FILE* in_file; 
    in_file = fopen("studentlist.txt","r");
    while(fscanf(in_file,"%d",&tmpid)!= EOF && count<DATA_SIZE)
    {
        fscanf(in_file,"%s",v.student[count]);
        //printf("%s\n",str );
        v.id[count]=tmpid;
        count++;
    }
    pointer =&v;
    return pointer;

}
int main()
{
    struct student *data;
    struct student *sdata;
    data = create_space(1);
    sdata = readData(data,list);
    //printf("%s\n",sdata->student[2] );

}
6
  • char *student[DATA_SIZE]; --> char student[DATA_SIZE][NAME_SIZE]; Commented Feb 6, 2017 at 1:13
  • pointer =&v;return pointer; : v is local variable. So Can't use this return value. Also pointer don't need as argument of this function. Commented Feb 6, 2017 at 1:15
  • How would I then assign the string value obtained from the text to the struct student[DATA_SIZE][NAME_SIZE]? Commented Feb 6, 2017 at 1:55
  • If v is a pointer, fscanf(in_file,"%s", v->student[count]);. If v isn't a pointer, fscanf(in_file,"%s", v.student[count]);. Commented Feb 6, 2017 at 1:58
  • i have another question, why can't i declare a variable char str[32]; within the function and do fscanf(in_file,"%s", str);, then v.student[count]=str;. It gives me an error. why is that? Commented Feb 6, 2017 at 2:14

1 Answer 1

1

Their are a couple of issues:

  • fscanf() reads formatted input, and returns the number of items read.

    This line:

    while(fscanf(in_file,"%d",&tmpid)!= EOF && count<DATA_SIZE)
    

    Could be this:

    while (count < DATA_SIZE && fscanf(in_file, "%d %255s", &list.id[count], str) == 2) {
    

    Which verifies that 2 values are being read on each line successfully.

  • You are not checking if in_file returns NULL. It's safe to do this. This goes the same for malloc().

  • You need to correctly create space for char *students[DATA_SIZE], as this is an array of char * pointers. Once you allocate space for this via malloc() or strdup(), then you can copy the contents into students.

    Here is an example of doing such a thing:

    while (count < DATA_SIZE && fscanf(in_file, "%d %255s", &list.id[count], str) == 2) {
    
        /* allocate space for one student */
        list.student[count] = malloc(strlen(str)+1);
        if (!list.student[count]) {
            printf("Cannot allocate string\n");
            exit(EXIT_FAILURE);
        }
    
        /* copy it into array */
        strcpy(list.student[count], str);
        count++;
    }  
    

Here is an example that you can use to help achieve your desired result:

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

#define DATA_SIZE 256

typedef struct {
    int id[DATA_SIZE];
    char *student[DATA_SIZE];
} studentinfo_t;

int main(void) {
    FILE *in_file;
    studentinfo_t list;
    char str[DATA_SIZE];
    size_t count = 0;

    in_file = fopen("studentlist.txt", "r");
    if (!in_file) {
        fprintf(stderr, "%s\n", "Error reading file");
        exit(EXIT_FAILURE);
    }

    while (count < DATA_SIZE && fscanf(in_file, "%d %255s", &list.id[count], str) == 2) {
        list.student[count] = malloc(strlen(str)+1);
        if (!list.student[count]) {
            printf("Cannot allocate string\n");
            exit(EXIT_FAILURE);
        }
        strcpy(list.student[count], str);
        count++;
    }

    for (size_t i = 0; i < count; i++) {
        printf("%d %s\n", list.id[i], list.student[i]);
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.