lundi 29 juin 2015

Reading text into a linked list using structs in C


I'm attempting to create a linked list using a struct from a txt file. Initially, I'm testing it with a txt file with only one line of information. This code compiles correctly, however when I run it, it returns "Line...didn't scan properly". As an aside, if I remove the if statement that returns such a value I get complete gibberish. I have no clue why the line isn't being scanned correctly, however I feel as though it may having something to do with the hyphen/plus sign in two of the terms that I tried to scan as strings. Thank you very much for any help you can provide.

This is the txt file:

1 20959U 90103A   14091.58762725 -.00000015  00000-0  00000+0 0  3197

This is the tester.c file:

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

typedef struct noradData {
    // line one
    int                lineNum;
    char             * satNum;
    char             * intDesig;
    float              epoch;
    float              firstTimeDeriv;
    char             * secondTimeDeriv;
    char             * drag;
    int                zero;
    int                set;
    struct noradData * next;
} Data; 

Data * first = NULL, * last = NULL;

int main() {
    char line[80], secondTimeDeriv[7], drag[7], satNum[6], intDesig[6];
    int lineNum, zero, set; 
    float epoch, firstTimeDeriv;

    FILE * fIn;
    Data * node;

    fIn = fopen("data1.txt", "r");
    if (fIn == NULL) {
        printf("Cannot open file\n");
        return 1;
    }

    while (fgets(line, sizeof(line), fIn) != NULL) {
        // Check line for various problems (too short, too long).
        if (line[0] == '\0') {
            printf ("Line too short\n");
            return 1;
        }

        if (line[strlen (line)-1] != '\n') {
            printf ("Line starting with '%s' is too long\n", line);
            return 1;
        }

        line[strlen (line)-1] = '\0';

        // Scan the individual fields.

        if (scanf("%d %s %s %f %f %s %s %d %d", &lineNum, satNum, intDesig, 
                &epoch, &firstTimeDeriv, secondTimeDeriv, drag, &zero, &set) 
                != 9) {
            printf ("Line '%s' didn't scan properly\n", line);
            return 1;
        }

        node = malloc(sizeof(Data));
        if (node == NULL) {
            printf ("Ran out of memory\n");
            return 1;
        }

        node->lineNum = lineNum;
        node->satNum = strdup(satNum);
        node->intDesig = strdup (intDesig);
        node->epoch = epoch;
        node->firstTimeDeriv = firstTimeDeriv;
        node->secondTimeDeriv = strdup(secondTimeDeriv);
        node->drag = strdup(drag);
        node->zero = zero;
        node->set = set;
        node->next = NULL;

        if (first != NULL) {
            last->next = node;
            last = node;
        }
        else {
            first = node;
            last = node;
        }
    }
    fclose (fIn);
    node = first;
    while (node != NULL) {
        printf("%d %s %s %f %f %s %s %d %d", node->lineNum, node->satNum, 
            node->intDesig, node->epoch, node->firstTimeDeriv, 
            node->secondTimeDeriv, node->drag, node->zero, node->set);
        node = node->next;
    }
    return 0;
}


Aucun commentaire:

Enregistrer un commentaire