简体   繁体   English

将文件传递到链表时,如何读取文件并确定其数据表示形式?

[英]How can I read a file and determine its data representation as I pass it into a linked list?

So in this code I'm trying to take a text file, grab all of string tokens and make nodes that are added into a linked list. 因此,在这段代码中,我试图获取一个文本文件,获取所有字符串标记,并使添加到链接列表中的节点成为可能。 I'm able to grab every individual string token from this, and make a node with the 'data' field filled in correctly. 我能够从中获取每个单独的字符串标记,并正确地填写“数据”字段,以创建一个节点。 The problem comes when I try and use the determinedohf function to fill in the 'type' field. 当我尝试使用确定的函数填充“类型”字段时,问题就来了。 I know in this code there is only one check for octal but even before then my gcc compiler is telling me there are incorrect casts to pointers/char that I'm not understanding. 我知道在这段代码中只有八进制的一次检查,但是即使在那之前,我的gcc编译器仍在告诉我,我不理解对指针/字符的强制转换。 What am I missing or not doing correctly in my determinedohf function? 我确定的函数中我缺少什么或做错了什么? I feel like parameters and such may also be incorrect, but I'm not sure how... 我觉得参数等可能也不正确,但是我不确定如何...

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

typedef struct node {

    char *data;
    char *type;
    struct node *next;

} node;


void readfile(char* filename) {

    FILE *file = fopen(filename, "r");

    if(file == NULL) {
        exit(1);
    }

    char buffer[51];

    while(fscanf(file, "%s", buffer) != EOF) {

        add(buffer);
    }

    fclose(file);
}

void add(char* line) {

    node *temp = malloc(sizeof(node));
    temp->data = strdup(line);
    temp->type = determine(line);
    temp->next = NULL;
    current = start;

    if(start == NULL) {
        start = temp;
    } else {
        while(current->next != NULL) {
            current = current->next;
        }
        current->next = temp;
    }
}

char* determineDOHF(node* line){

      /*supposed to determine whether the string is represented as
      decimal, octal, hex, or floating point*/
    char *dohfType;
    char input[51] = line;
    if(input[0] == '0'){
        dohfType = 'Octal';
    }

    return dohfType;

}

3 things: 3件事:

  1. In your add function, it would be a good idea to always free memory after doing a malloc. 在您的add函数中,最好在执行malloc之后始终释放内存。 Malloc is going to allocate memory to your program, which is only going to be freed if you manually do it, or close the program. Malloc将为您的程序分配内存,只有在您手动执行或关闭程序后才会释放该内存。

  2. Also, in determinedohf you are returning a pointer to a variable local to that function. 此外,在determinedohf您将返回指向该函数局部变量的指针。 That memory location may not be valid once the function returns, and you'd be reading garbage values. 函数返回后,该内存位置可能无效,并且您将读取垃圾值。 If you want to use that returned string, you'd want to use malloc to allocate the input string. 如果要使用该返回的字符串,则要使用malloc分配input字符串。 I'd strongly advise that you replace the type with an integer instead and use an enumeration to improve readability. 我强烈建议您用整数代替类型,并使用枚举来提高可读性。

    enum types { OCTAL=0, DECIMAL, HEX } 枚举类型{OCTAL = 0,DECIMAL,HEX}

  3. You are assigning a type of node * to a type of char which is an invalid assignment. 您正在将node *的类型分配给char类型,这是无效的分配。 I suspect you want to use the type element of node * . 我怀疑您想使用node *的类型元素。 So you should be doing something like this: 因此,您应该执行以下操作:

    strcpy(input, link->type) strcpy(输入,链接->类型)

In C, you can't just assign strings after initialization of a variable. 在C语言中,您不能仅在变量初始化后分配字符串。 You need to use strcpy to do so. 您需要使用strcpy这样做。

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

相关问题 如何创建 (C) 函数以使用“读取”将文件中的数据读取到链表中? - How can I create a (C) function to read data from a file into a linked-list using 'read'? 如何将任何文件读入链接列表? - How can I read any file into a linked list? 在C语言中,我如何从文本文件中读取数据并将其插入一个链接列表,该链接列表根据名字按升序排列? - In C, how can i read data from a text file and insert them an linked list that organised in an ascending order according to the first names? 如何创建从文件中读取的字符的链接列表? C语言 - How can I create a linked list of characters read from a file? C language 如何使用指针将矩阵文本文件读取到C中的链接列表中? - How can i read a matrix text file into a linked list in C using pointers? 如何显示在链接列表中创建的数据? - How can I display the data I created in a linked list? 如何修改链表的 header 文件以处理多种类型的数据? - How can i modify my header file of linked list to work with many type of data? 如何将字符串从文件复制到 C 中的链表? - how can i copy string from file to a linked list in C? 如何将文本文件读入结构并与其成员进行交互? - How can I read a text file into a structure and interact with its members? 无法将文件中的数据读入链表 - Can't read data from file into a linked list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM