Reading Data From File to Linked List

This is a discussion on Reading Data From File to Linked List within the C Programming forums, part of the General Programming Boards category; The data from the text file looks like: 40T Trapp, Dave; 3.9 pin# Last_Name, First_Name; gpa I am trying to ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    46

    Reading Data From File to Linked List

    The data from the text file looks like:
    40T Trapp, Dave; 3.9
    pin# Last_Name, First_Name; gpa

    I am trying to read them into a linked list composed of the structure:
    Code:
    typedef struct
    {
        char pin[4];
        char *name;
        double gpa;
    }   STU;
    So far the function I used to read the data from the file worked successfully for my test file which was only contained integers and placed them into a sorted linked list.
    My only concern now is modifying it to accommodate the data I need.
    I was told that I should use fgets and sscanf but I am not too certain of how to go about doing this, especially since we only covered sscanf very briefly so I am still not very sure of what it does and I can't find very clear examples online.
    Here's the original function:
    Code:
    int getData (FILE* fpData, STU* pData)
    {
        int ioResult;
    
        ioResult = fscanf (fpData, "%d ", pData->test);
        if (ioResult == 1)
           return 1;
        else
           return 0;
    }    // getData
    I am calling it inside of a larger function which builds the sorted list.

    Any and all feedback is appreciated

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    sscanf() works just like scanf(), except you add one parameter, which is the name of the char array you are scanning, first:

    fgets() is a lovely way to get a row of text:
    Code:
    sscanf(filePointerName, "%YourFormatSpecifierHere",&AddressOfYourVariableReceivingTheValue); //&number (or arrayName with no &)
    fgets(charArrayName, sizeof(charArrayName), filePointerName);
    The trick is to always keep a larger than you ever will need array size, (waste a tiny bit of memory to keep everything running well), and you will have a newline ('\n') and an end of string char, added to your char array[].

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    46

    First try

    So here's how I've implemented it thus far:
    Code:
    int getData (FILE* fpData, STU* pData)
    {
    //    Local Definitions
        int ioResult;
        char temp[MAX_SIZE];
    
    //    Statements
        fgets(temp, sizeof(temp), fpData);
        sscanf(temp, "%4s", pData->pin);
        sscanf(temp + sizeof(pData->pin), "%[^;]", pData->name);
        sscanf(temp + sizeof(pData->name), "%lf", pData->gpa);
        printf("%s pin :%s name :%s gpa:%4.2lf\n", temp, pData->pin, pData->name, pData->gpa);
        ioResult = 1;
        system("pause");
        if (ioResult == 1)
           return 1;
        else
           return 0;
    }    // getData
    It successfully scans in the pin and the name, albeit it without the semi-colon at the end. It seems that doing this screws up the data that comes afterward because when I try to print the gpa it gives me a bunch of garbage.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How big is MAX_SIZE ... your input data is probably being clipped, make the buffer bigger...

    The usual technique is to make your buffer bigger than you'll ever need for a single line... If your file has lines that are (for example) 80 char max... make your buffer 128 bytes.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    46
    MAX_SIZE is initialized to 1000..

    I know I need to make some more changes because I also need to dynamically allocate memory for the names before I initialize them

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    46
    K so I managed to successfully get all the data I wanted into the structure
    Code:
    int getData (FILE* fpData, STU* pData)
    {
    //    Local Definitions
        int ioResult;
        char temp[MAX_SIZE];
        char *pGpa, *pEnd, *pName;
    
    //    Statements
        fgets(temp, sizeof(temp), fpData);
    
        sscanf(temp, "%4s", pData->pin);
    
        pGpa = strrchr(temp, ';') + 1;
        pData->gpa = strtod(pGpa, &pEnd);
    
        *pGpa = '\0';
        pName = temp + sizeof(pData->pin);
        pData->name = (char *)calloc (strlen(pName) + 1, sizeof(char));
        if(!pData->name)
            printf("Error! Out of memory\n");
        strcpy(pData->name, pName);
    
        printf("%s %s %.2lf\n", pData->pin, pData->name, pData->gpa);
        ioResult = 1;
        system("pause");
        if (ioResult == 1)
           return 1;
        else
           return 0;
    }    // getData
    Don't know if there is an easier way to do this...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading in file to linked list?
    By rickyson49 in forum C Programming
    Replies: 2
    Last Post: 03-31-2011, 10:07 AM
  2. Reading Text file in Linked list!
    By satty in forum C Programming
    Replies: 20
    Last Post: 07-29-2010, 09:48 AM
  3. Reading from a file into a linked list.
    By Wiretron in forum C Programming
    Replies: 5
    Last Post: 01-23-2006, 08:24 AM
  4. Reading a file into a Linked List
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-20-2002, 08:08 AM
  5. reading from a file to linked list
    By opacity in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2002, 10:56 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21