如何从结构中的文本文件中读取数据,在c编程中使用不同数量的单词?

时间:2016-03-16 18:37:17

标签: c struct linked-list

我正在尝试从文本文件中读取struct中的数据,我希望将其作为原始文件打印出来。

这是我的代码:

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

typedef struct _Member{
    char *name;
    int age;
    int id;
    char *town;
}Member;

int main() {

    FILE *file = fopen("member.txt", "r");
    FILE *newFile = fopen("new_member.txt", "w");

    int counter=0, line = 10;

    Member *member = malloc(sizeof(Member)*line);

    for(int i =0; i<line; i++){
        int len = 12;
        char *name = malloc(sizeof(char)*(len+1));
        name [len] = '\0';
        int age;
        int id;
        char *town = malloc(sizeof(char)*(len+1));
        town [len] = '\0';

        fscanf(file, "%s %i %i %s\n", name, &age, &id, town);

        member[counter].name = name;
        member[counter].age =age;
        member[counter].id = id;
        member[counter].town = town;

        counter++;
    }

    for(int i = 0; i < counter-1; i++){

        fprintf(newFile, "%s %d %d %s\n", member[i].name, member[i].age, member[i].id, member[i].town);
    }

    fclose(file);
    fclose(newFile);

    printf("Successful\n");

    return 0;
}

member.txt

John 23 3456 Edinburgh
Robert 33 4567 London
Peter 45 4345 
Cary 56 12345 
Dian 45 4587 Norfolk
Will 32 3453 Essex
Paul 56 7896 York
Alisha 78 1234 Peterbourgh
Brandon 45 3214 Brighton
Eli 39 5234 Oxford

我得到了结果。 new_member.txt

John 23 3456 Edinburgh
Robert 33 4567 London
Peter 45 4345 Cary
56 12345 4345 
Dian 45 4587 Norfolk
Will 32 3453 Essex
Paul 56 7896 York
Alisha 78 1234 Peterbourgh
Brandon 45 3214 Brighton

如何打印与原始文件相同?请注意;我正在尝试读取STRUCT中的数据。我的结构是阅读第三行并从第四行取名为城镇。任何帮助将不胜感激。感谢

3 个答案:

答案 0 :(得分:0)

您尚未为少数情况定义城镇名称,例如

fscanf

但是当你尝试通过fscanf(file, "%s %i %i %s\n", name, &age, &id, town); 读取它时,编译器期望城镇名称存在;

fgets

这些数据将来自下一行,依此类推。

如果是这种情况,那么您可以使用sscanfvar possibleContexts = ['2d', 'webgl', 'webgl2', 'experimental-webgl', 'experimental-webgl2', ...]; var availableContexts = possibleContexts.filter(function(c) { return canvas.getContext(c); }); 代替。

希望这有帮助。

答案 1 :(得分:0)

这种方法有效,但只有在城镇中的单词不包含在文本的一行中时才会有效。

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

typedef struct _Member{
    char *name;
    int age;
    int id;
    char *town;
}Member;

int main() {

FILE *file = fopen("member.txt", "r");
FILE *newFile = fopen("new_member.txt", "w");
char* lines;
lines = (char *)malloc(40*sizeof(char));
int counter=0, line = 10;
int i;
Member *member = malloc(sizeof(Member)*line);

for(i=0; i<line; i++){
    int age;
    int id;
    while(fgets(lines, 40, file)!=NULL){

        char *name = (char *)malloc(13*sizeof(char));
        char *town = (char *)malloc(13*sizeof(char));
        if(sscanf(lines, "%s %i %i %s", name, &age, &id, town)==4){
            member[counter].name = name;
            member[counter].age =age;
            member[counter].id = id;
            member[counter].town = town;
            counter++;
        }
        else{
            member[counter].name = name;
            member[counter].age =age;
            member[counter].id = id;
            member[counter].town = "\0";
            counter++;
        }
    }
}

for(i= 0; i < counter; i++){

    fprintf(newFile, "%s %d %d %s\n", member[i].name, member[i].age, member[i].id, member[i].town);
}

fclose(file);
fclose(newFile);

printf("Successful\n");

return 0;

}

如果您愿意,可以更改此内容:

            else{
            member[counter].name = name;
            member[counter].age =age;
            member[counter].id = id;
            member[counter].town = "\0";
            counter++;
            }

到此:

            else{
            printf("The line of %s is not valid", name);
            }

所以你的新文字只包含有效的行。

答案 2 :(得分:-1)

尝试使用getline(),strtok(),atoi()来获取数据。