简体   繁体   中英

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. 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:

  1. In your add function, it would be a good idea to always free memory after doing a 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.

  2. Also, in determinedohf you are returning a pointer to a variable local to that function. 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. 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 }

  3. You are assigning a type of node * to a type of char which is an invalid assignment. I suspect you want to use the type element of node * . So you should be doing something like this:

    strcpy(input, link->type)

In C, you can't just assign strings after initialization of a variable. You need to use strcpy to do so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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