简体   繁体   English

使用 fscanf (C) 将文本文件中的字符串保存到结构中

[英]Saving a string from a text file to a struct using fscanf (C)

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.我正在尝试从文本文件中获取 id 和字符串并将其保存到结构中。 The id is saving fine but the string isn't. id 保存得很好,但字符串不是。

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] );

}

Their are a couple of issues:他们有几个问题:

  • fscanf() reads formatted input, and returns the number of items read. fscanf()读取格式化的输入,并返回读取的项目

    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.这验证了在每行上成功读取了2值。

  • You are not checking if in_file returns NULL .您不是在检查in_file返回NULL It's safe to do this.这样做是安全的。 This goes the same for malloc() .这对于malloc()也是一样的。

  • You need to correctly create space for char *students[DATA_SIZE] , as this is an array of char * pointers.您需要为char *students[DATA_SIZE]正确创建空间,因为这是一个char *指针数组。 Once you allocate space for this via malloc() or strdup() , then you can copy the contents into students .一旦您通过malloc()strdup()为此分配了空间,您就可以将内容复制到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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM