Thread: Transferring text from file into linked list

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

    Transferring text from file into linked list

    I have to write a program for my current project that will open a file and copy the text into a linked list word by word.

    The way I was thinking of doing it was by copying the text into an array and using the spaces between the words to seperate each word and store it in the list.

    I was just wondering whether this is the best way of doing it as I am going to have to have a massive array to store all the text (as it is 156,000 characters).

    Thanks in advance

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Because file is not too long I think to read the whole file in the buffer and process buffer in a way you have described is better than to read word by word...

    So I don't see a problem in the described approach
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    What u could do is read a single line at a time. tokensize the string and store them in the linked list. Follow the same till u reach the EOF while reading the file. Use fgets to read a line.

    ssharish2005

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    im not exaclty sure what you mean by buffer, I was planning to read the whole text file into an array. Then when inserting it into the linked list I will split it into seperate words, this is because I need to be able to search the list to check how many times a word appears in the text.

    I also need to be able to dynamically allocate the memory for the array, as my program has to be able to cope with the user inputting the name of the file.

    I have written the code below to do this, but I am sure this is incorrect as the first piece seg faults at the while statement and the second piece doesn't compile as the putc 2nd statement makes a pointer from an integer without a cast.

    Code:
    char readdata(char *name){
         char *x;
         x = calloc(1, sizeof(FILE));
         
                   
         FILE *ifp;
         int i = 0;
         ifp = fopen(name, "r");
         
         while ((x[i] = getc(ifp)) != EOF){
               i++;
               }
         
         return *x;
    }
    Code:
    char readdata(char *name){
         char *x;
         x = calloc(1, sizeof(FILE));
                        
         FILE *ifp;
         int i = 0;
         char c;
         ifp = fopen(name, "r");
         
         while ((c = getc(ifp)) != EOF){
               putc(c, x[i]);
               i++;
               
               }
         
         return *x;
    }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    sizeof FILE returns number of bytes in the structure FILE, it has nothing to do with the file length
    use
    Code:
    fseek(fptr, 0, SEEK_END);
    size = ftell(fptr);
    pair to determine the actual length of the open file.

    Before using file pointer - check if it is not null
    read the entire content of the file with fread (don't forget to check the return value)
    before using allocated memory - check that the pointer returned by calloc is not null
    don't forget to allocate one additional byte to put the 0 character at the end if you're going to work with the array as a string
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    thanks a lot for your help, I did doubt whether the calloc statement was correct. I'll give that a go and see what I come up with.

    Thanks again for pointing me in hte right direction

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    [Code]
    ifp = fopen(name, "r");
    [\code]

    is this line in the program before correct?

    I seem to be getting a segmentation fault as a result of this line not working correctly, it is supposed to open the file that's name is stored in the char field name which has been passed into this function.

    I have the text file in the same folder as program is running from although this is in my documents and not at the root, does this make any difference.

    Thanks again

    edit: thanks for the headsup dwks, I was leaving it till the end until I knew the exact program structure so I know when to free the memory but I will comment in a reminder now so I don't forget

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If the file couldn't be opened, fopen returns NULL. Since you don't check for that, it could quite conceivably cause a segmentation fault.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    Is it possible to pass pointers between functions?

    the readdata function has been fixed so it works properly now but I can't seem to get it to pass a pointer back to the main function, I have included the code in my main function below.

    Thanks

    Code:
    int main(){
        char a[30];
        char *y;
        scanf("%s", a);
        *y = readdata(a);
        return 0;
    }
    Edit: I realise I haven't allocated any space for y, but because it is a pointer shouldn't it just be able to use the space allocated for the array in the function readdata
    Last edited by manutdfan; 01-10-2007 at 09:52 AM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is it possible to pass pointers between functions?
    Yes, do whatever you like - declarations and allocations permitting.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include<stdio.h>
    
    char *getdata()
    {
         char *str = "hello";
         
         return str;
    }
    int main()
    {
        char *p;
        
        p = getdata();
        
        printf("%s",p);
        
        getchar();
        return 0;
    }
    The sample code gives you the solution for your problem. I terms of returning the char * pointer to the main.

    ssharish2005

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    getdata should return const char*
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And str itself should be const, because string literals are constant.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    Thanks, I have it working now. I just have a question about freeing the calloced memory.

    Because I need to use this in the main program for furthur operations, how do I go about freeing it. Can I just free the memory by freeing the array that I use in the main program like so:

    Code:
    int main(){
        char a[30];
        char *y;
        scanf("%s", a);
        y = readdata(a);
        printf("%s", y);
        free(y);
        return 0;
    }
    edit: the scanf statement is for the input of the file name to be used (just incase this is unclear).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM