Reading a TextFile into a Linked List

This is a discussion on Reading a TextFile into a Linked List within the C Programming forums, part of the General Programming Boards category; Hi guys, Need some help reading a space delimited text file into a linked list. The text file looks like ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    Reading a TextFile into a Linked List

    Hi guys,
    Need some help reading a space delimited text file into a linked list.

    The text file looks like this:
    012452 John Smith 23.56
    891756 Joe Bloe 24.94
    268174 Sample Smith 18.56
    718756 Terry Totts 27.86
    261786 James Packer 25.98

    Each record is one line in the above file. I need to read in 4 fields from the above:
    int empNo; char firstname; char lastame; double rate;

    How do I read one line at a time until the EOF, while at the same time capturing each of the four fields per record.

    The catch is, I must not use an array!

    Please let me know if anyone can help me start this code.

    Cheers!

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well, first_name and last_name have to be arrays. Or pointers. And if that array catch does not encompass pointers than you could use fgets and memory allocated with malloc or similar. Does it seem like the teacher would want you to read in the data character by character and do all the conversions yourself? If so, are there any guarantee's other than four fields being space delimited that you could think of to make it easier for yourself?

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by cisco_leon
    How do I read one line at a time until the EOF
    I prefer to think of it as, "while successfully reading the intended data".
    http://www.daniweb.com/techtalkforum...155265-18.html
    Quote Originally Posted by cisco_leon
    The catch is, I must not use an array!
    That would be a reasonable assumption considering the intent is to implement a linked list.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Quote Originally Posted by Tonto
    Well, first_name and last_name have to be arrays. Or pointers. And if that array catch does not encompass pointers than you could use fgets and memory allocated with malloc or similar. Does it seem like the teacher would want you to read in the data character by character and do all the conversions yourself? If so, are there any guarantee's other than four fields being space delimited that you could think of to make it easier for yourself?
    Hi,
    Sorry there doesn't seem to be any other guarantees in the spec I've been given.

    I guess, the first question for me to find an answer to is:
    1. Is it possible to load records from such a text file, straight into a linked list, without the use of arrays? if so, what would the pseudo code look like?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Have a structure that represents a single record. Read data into the structure. Add the structure to the list.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure. However, you will either need to have written the pointers (or space they take up anyway) to disk also when you have created the file, or you need to have a seperate structure which just contains the useful information. If the latter, your linked list will end up looking something like:
    Code:
    struct node
    {
        struct node *next, *prev;
        struct data d;
    };
    Then you read into the 'data' structure directly from disk.
    Code:
    fread( &node->d, sizeof( node->d ), 1, fp );
    You follow these steps:
    Code:
    while not done
        make a new node
        read into the node
        if no error
            link node to list
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Quote Originally Posted by Dave_Sinkula
    Have a structure that represents a single record. Read data into the structure. Add the structure to the list.
    Hi Dave,
    1. Have a structure that represents a single record.
    Code:
    struct charRecord
    {
       char letter;
       int empNum;
       char lastName;
       char firstName;
       double hourRate;
       struct charRecord *next;
    };
    
    typedef struct charRecord charNode;
    2. Read data into the structure.
    Here is where I get stuck. Specifically the "read data" part.

    3. Add the structure to the list.
    If it was an array of strings, I can do it, but avoding arrays I don't know how.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For starters, your record is wrong, unless everyone you know only has a single letter for their first and last name. Once you fix that, go read up on the file chapter of your book, or read up on fopen, fread, fwrite, fgets, fscanf, fclose, and all the other f-ing functions.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Only because I think you'll do it wrong and I'm just trying to skip to the chase with your linked list, I'll give you this little early Christams present with the file reading.
    Code:
    #include<stdio.h>
    
    struct record
    {
       char   letter;
       int    empNum;
       char   lastName[20];
       char   firstName[12];
       double hourRate;
    };
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line[BUFSIZ];
          struct record record;
          while ( fgets(line, sizeof line, file) != NULL &&
                  sscanf(line, "%d %11s %19s %lf", 
                         &record.empNum, record.firstName, 
                         record.lastName, &record.hourRate) == 4 )
          {
    #ifndef NDEBUG /* while debugging */
             printf("%10d %-20s %-12s %g\n", 
                    record.empNum, record.lastName, 
                    record.firstName, record.hourRate);
    #else /* real code */
             /* add 'record' to linked list */
    #endif
          }
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* my output
         12452 Smith                John         23.56
        891756 Bloe                 Joe          24.94
        268174 Smith                Sample       18.56
        718756 Totts                Terry        27.86
        261786 Packer               James        25.98
    */
    So basically, fgets & sscanf.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Quote Originally Posted by quzah
    For starters, your record is wrong, unless everyone you know only has a single letter for their first and last name. Once you fix that, go read up on the file chapter of your book, or read up on fopen, fread, fwrite, fgets, fscanf, fclose, and all the other f-ing functions.


    Quzah.
    Ok thanks will do. Before I go on, is this correct data structure?

    Code:
    struct charRecord
    {
       int empno;
       char lname[];
       char fname[];
       double rate;
       struct charRecord *next;
    };
    
    typedef struct charRecord employee
    Please let me know.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Quote Originally Posted by Dave_Sinkula
    Only because I think you'll do it wrong and I'm just trying to skip to the chase with your linked list, I'll give you this little early Christams present with the file reading.
    Code:
    #include<stdio.h>
    
    struct record
    {
       char   letter;
       int    empNum;
       char   lastName[20];
       char   firstName[12];
       double hourRate;
    };
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line[BUFSIZ];
          struct record record;
          while ( fgets(line, sizeof line, file) != NULL &&
                  sscanf(line, "%d %11s %19s %lf", 
                         &record.empNum, record.firstName, 
                         record.lastName, &record.hourRate) == 4 )
          {
    #ifndef NDEBUG /* while debugging */
             printf("%10d %-20s %-12s %g\n", 
                    record.empNum, record.lastName, 
                    record.firstName, record.hourRate);
    #else /* real code */
             /* add 'record' to linked list */
    #endif
          }
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* my output
         12452 Smith                John         23.56
        891756 Bloe                 Joe          24.94
        268174 Smith                Sample       18.56
        718756 Totts                Terry        27.86
        261786 Packer               James        25.98
    */
    So basically, fgets & sscanf.
    Thanks Dave! Appreciate, will give that a go.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    and all the other f-ing functions


    What strikes me as odd is that the OP has insisted on no arrays, but here you go...
    Code:
          char line[BUFSIZ];
    Code:
       char   lastName[20];
       char   firstName[12];
    ...arrays.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  13. #13
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    All those arrays should be char*, which I guess you could then allocate 20*sizeof(char) if you wish to accomplish a similar effect.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by jafet
    What strikes me as odd is that the OP has insisted on no arrays, but here you go...
    Code:
          char line[BUFSIZ];
    Code:
       char   lastName[20];
       char   firstName[12];
    ...arrays.
    I interpreted the assignment as, "Reading a TextFile into a Linked List". That is, using a linked list of structures rather than an array of structures.

    If you think instead the intent is to completely avoid an array of any kind, including C-strings, then offer some coding help in that direction.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:16 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 06:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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