Reading from a file into a linked list.

This is a discussion on Reading from a file into a linked list. within the C Programming forums, part of the General Programming Boards category; I've opened up the txt document using the fopen function, and am trying to get the program to 'read' the ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Reading from a file into a linked list.

    I've opened up the txt document using the fopen function, and am trying to get the program to 'read' the file.

    I'm trying to get it to read the txt file 1 character at a time, but am not entirely sure how to do this, would I used

    Code:
    i = getchar();
    while (i != EOF) { 
    fscanf(fp,"%c",a); 
    i++;
    }

    Whereby a and i are integers.

    How would I the go on to store this information into a linked list?

    Thanks in advance for any help.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    use fgetc instead, it takes a file pointer, prototype is as follows:

    Code:
    int fgetc (FILE *fp);
    i've never used this code myself, but i understand that it is suited to this type of situation. there's a tutorial on File I/O on this site, that's where i took this from, follow this link for more info on File I/O - there's also a link to an example using fgetc on the page

    http://www.cprogramming.com/tutorial/cfileio.html
    Last edited by Richie T; 01-23-2006 at 06:21 AM. Reason: update - example
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Thanks for that, I've got it reading the txt file character by character.

    How would I then go about storing this into a linked list?

    I want to store it word by word.

    If possible could someone link me to a useful tutorial that is relelvant to this?

    Thanks
    Last edited by Wiretron; 01-23-2006 at 06:34 AM.

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    i'm not familiar with linked lists, but my guess is that since you are reading in character by character, you should be able to create a new node on your list when you detect the ASCII space character (decimal 32). once you detect this, you read the next character into a new node untill you detect another space, and so on.

    maybe someone else would be able to assist you in creating linked lists, i've never used them myself, but i'm guessing that you might be familiar with them yourself already. hope that this helps a little at least.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Ok thanks for your help, I understand the concept of what you're saying, just gotta work out how to do linked lists, and how to create a new node when a space is detected etc!

    If anyone has a useful tutorial or some advice, please post!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Code:
    int ch;
    while ( (ch=fgetc(fp)) != EOF ) {
      // do something with ch
    }
    As for linked lists, there's plenty of examples to look at if you do a board search.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM

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