从文件中读取数据并将其写入链接列表 [英] Reading a data from a file and writing it to a linked list

查看:54
本文介绍了从文件中读取数据并将其写入链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
struct Node{
    char *name;
    int time;
    int sec; 
    int x;
    int y;
    struct Node *next;
};
int main(){
    FILE *file;
    int index;
    struct Node *data=malloc(sizeof(struct Node));
    struct Node *tmp=data,*tmp2=data;
    int enter;

    file=fopen("data.txt","r");

    if(file==NULL){
        printf("File was not opened!\n");
        return 0;
    }

    while(!feof(file)){
        tmp=malloc(sizeof(struct Node));
        fscanf(file,"%d",&index);
        fscanf(file,"%d",&tmp->time);
        fscanf(file,"%d",&tmp->sec);
        fscanf(file,"%d",&tmp->x);
        fscanf(file,"%d",&tmp->y);
        fscanf(file,"%s",tmp->name);
        fscanf(file,"%'\0",&enter);
        tmp->next=NULL;
        tmp=tmp->next;
    }
    fclose(file);
    while(tmp2 != NULL){
        printf("file:%d\t%d\t%d\t%d\t%s\n",tmp2->timestamp,tmp2->sec,tmp2->pointx,tmp2->pointy,tmp2->name);
        tmp2=tmp2->next;
    }
    return 0;
}

需要一些帮助来从文件中读取数据并将它们写入链表,然后将链表打印到secreen.但是在我启动程序后它立即停止工作.在文件中的数据是这样的:

Need some help with reading data from a file and write them to the linked list and after that print the linked list to the secreen.But it stops working immedately after I start the program.In the file data is like that:

  • 1 28000 41 29 50 bbb
  • 2 29000 91 19 60立方厘米
  • 3 30000 23 77 92 ddd
  • 4 30000 37 62 65 eee
  • 5 31000 14 45 48 fff

(它们之间有标签)

我读了很多问题,但他们的答案并没有帮助我.我认为我在某处遗漏了一点,但看不到问题,这是有关直接将数据读取到链表或其他内容吗?感谢帮助.//我正在查看代码,再次感谢您的帮助**(已编辑)

I read many questions but their answers didn't help me.I think I'm missing a point somewhere but I couldn't see the problem.Is it about reading a data directly to the linked list or something else? Thanks for help. //I'm looking at my code againt thanks for help**(edited)

推荐答案

name 字段是一个指针,但是您永远不会分配它.所以

The name field is a pointer, but you never allocate it. So

    fscanf(file,"%s",tmp->name);

未定义的行为 .您确实应该非常害怕.花几个小时阅读Lattner的博客: 每个C程序员应该了解的未定义行为 .

is undefined behavior. You really should be very scared. Take a few hours to read Lattner's blog: what every C programmer should know about undefined behavior.

也许您可以使用 getline(3)在POSIX系统上读取行,或者使用 fgets(3).

Perhaps you might use getline(3) if on a POSIX system to read a line, or else use fgets(3).

顺便说一句,当我编译带有所有警告的代码时调试信息(如果使用 GCC ,则使用 gcc -Wall -g )许多错误和警告.您应该改进代码,直到没有警告.然后,您应该使用调试器( gdb )查找许多其他错误.您将能够逐步运行您的越野车程序并查询一些变量.您应该在黑板上(或纸上)绘画,您认为 C动态内存分配

BTW, when I compile your code with all warnings & debug info (with gcc -Wall -g if using GCC) I'm getting many errors and warnings. You should improve your code till you get no warnings. Then you should use debugger (gdb) to find many other bugs. You'll be able the run your buggy program step by step and query some variables. You should draw on a chalkboard (or on a paper) what you believe is the model and shape of the memory (including heap) of the process running your program. You'll find several bugs. Read more about C dynamic memory allocation and virtual address space.

此外,使用请阅读您正在使用的每个功能的文档,尤其是 fscanf .

Please read the documentation of every function that you are using, notably for fscanf.

请注意,您忘记处理 malloc 的失败;并且我还建议在失败的情况下使用 perror(3).因此,至少将 tmp = malloc(sizeof(struct Node)); 替换为

Notice that you forgot to handle failure of malloc; and I also recommend using perror(3) in failure cases. So at least replace tmp=malloc(sizeof(struct Node)); with

tmp = malloc(sizeof(struct Node));
if (!tmp) { perror("malloc Node"); exit(EXIT_FAILURE); };

PS.希望您不要期望我们做作业.您的程序有很多错误,但是您可以通过自己找到 这些错误来学到很多东西.问别人会适得其反.

PS. I hope you don't expect us to do your homework. Your program is extremely buggy, but you'll learn a lot by finding by yourself these bugs. Asking someone else is counterproductive.

这篇关于从文件中读取数据并将其写入链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆