Thread: how do I read a text file in C? then make an array of linked lists from it?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    how do I read a text file in C? then make an array of linked lists from it?

    say for a each line in the text file there are several words. I want to read each word, which is separated by space. how do i do that? how do i get each word in the line? each line is treated like a linked list and i want to store it in an array. but how?

    for example the text is

    jeffrey jake john
    charlie emilio martin
    drew lucy cameron

    i want jeffrey jake and john to be a linked list. charlie emilio martin, another. drew lucy and cameron... so on. each line is a linked list

    then i would store these linked list to an array.

    but before i can do that. i must know how to read each word in a line.

    please help...

    thanks

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    1) First you open the file with fopen
    2) Then you get a line using fgets
    3) Then you parse the line using sscanf
    4) Then you close the file with fclose
    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

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    The following Code opens a given file and reads the names and puts them in a list.
    What you want is a list of lists, something like an array of lists. Think of it and i will help you get the final answer to your problems.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef struct _Node
    {
    	char *name;
    	struct _Node *next;
    }Node;
    
    typedef struct _List
    {
    	int ListSize;
    	Node *head;
    	Node *tail;
    }List;
    
    Node *CreateNode(char *name)
    {
    	Node *n = (Node *)calloc(1, sizeof(Node));
    	if(n)
    	{
    		n->name = strdup(name);
    		n->next = NULL;
    
    		return n;
    	}
    	else
    		return NULL;
    }
    
    List *CreateList()
    {
    	List *l = (List *)calloc(1, sizeof(List));
    	if(l)
    	{
    		l->ListSize = 0;
    		l->head = NULL;
    		l->tail = NULL;
    		
    		return l;
    	}
    	else
    		return NULL;
    }
    
    int ListInsertAtEnd(List *l, char *name)
    {
    	if(l)
    	{
    		Node *n = CreateNode(name);
    		if(n)
    		{
    			if(!l->head && !l->tail)
    			{
    				l->head = n;
    				l->tail = n;
    			}
    			else
    			{
    				l->tail->next = n;
    				l->tail = n;
    			}
    			//Advance the list size.
    
    			l->ListSize++;
    
    			//Return the Value.
    			return 0;
    		}
    		else
    		{
    			printf("Memory Error.\n");
    			return -1;
    		}
    	}
    	else
    	{
    		printf("List is NULL.\n");
    		return -2;
    	}
    }
    void DumpList(List *l)
    {
    	Node *n = NULL;
    	printf("List has size &#37;d\n", l->ListSize);
    	for(n = l->head; n != NULL; n = n->next)
    		printf("%s ", n->name);
    	printf("\n");
    }
    
    void DeleteList(List *l)
    {
    	Node *p = NULL;
    	Node *n = l->head;
    	while(n)
    	{
    		p = n->next;
    		free(n->name);
    		free(n);
    		n = p;
    	}
    	free(l);
    	l = NULL;
    }
    List *CreateListFromFile(char *filename)
    {
    	FILE *file = NULL;
    	if(!filename)
    	{
    		printf("Enter a valid filename.\n");
    		return NULL;
    	}
    	else
    	{
    		List *l = CreateList();
    		if(!l)
    		{
    			printf("Error in Creating an Empty list.\n");
    			return NULL;
    		}
    		else
    		{
    			if(!(file = fopen(filename, "r")))
    			{
    				printf("Error can not open %s file.\n", filename);
    				DeleteList(l);
    				return NULL;
    			}
    			else
    			{
    				char *buffer = (char *)calloc(BUFSIZ, sizeof(char));
    				if(!buffer)
    				{
    					printf("Can not create Buffer.\n");
    					DeleteList(l);
    					return NULL;
    				}
    				else
    				{
    					char *token = NULL;
    					while(fgets(buffer, BUFSIZ, file) != NULL)
    					{
    						if(buffer[0] == '\0')
    							break;
    						if(buffer[strlen(buffer) -1] == '\n')
    							buffer[strlen(buffer) -1] = '\0';
    						token = strtok(buffer, " ");
    						if(token)
    						{
    							ListInsertAtEnd(l, token);
    							while(token)
    							{
    								token = strtok(NULL, " ");
    								if(token)
    									ListInsertAtEnd(l, token);
    							}
    						}
    						memset(buffer, 0 , BUFSIZ);
    					}
    					if(feof(file))
    					{
    						fclose(file);
    						free(buffer);
    						return l;
    					}
    					else
    					{
    						printf("Earlier break, the file is not read till the end.\n");
    						fclose(file);
    						free(buffer);
    						DeleteList(l);
    						return NULL;
    					}
    				}
    			}
    		}
    	}
    }
    
    int main(int argc, char **argv)
    {
    	List *l = CreateListFromFile("test.txt");
    	DumpList(l);
    	return 0;
    }
    The code was writen in Visual Studio 2005, of course i suppose there are bugs..

    Printed results using file "test.txt"

    List has size 5
    kostas nikos ilias baggelis giannis
    Press any key to continue . . .

    The names are in greek, can change them to anything, use a file named test.txt in your project directory and give names.

    e.g "test.txt"

    kostas niikos ilias baggelis giannis\n
    Last edited by Bokarinho; 07-24-2007 at 01:37 PM.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    hmmm.. how do i exactly read by line then parse it? i don't know the syntax...

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Read the first reply over and check the links out.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    Code:
    hmmm about the linked lists, is this correct? 
    
    typedef struct UrlList    
            {char *url;
             struct UrlList *Unext;
             } SiteList;
    
     UrlList *uHead, *uTail;
    
     main()
     {
     char name[2];
     name[0] = "john";
     name[1] = "mark";
     name[2] = "matthew";
    
    for (i=0; i=2; i++)
     {
     char *newword = name[i];
     UrlList *newnode = malloc (sizeof(UrlList));  // should i put SiteList after UrlList?
        newnode->next = NULL;
        uHead = newnode;
        uTail = uHead;
      }
    ---if i access each element, is this correct?
    
    UrlList *tmp;
         tmp = head;
         while (tmp!=NULL)
         {
               printf ("%s \n ", tmp->url);
               tmp = tmp->next;
         }
     else
      {
        newnode->url = newword;
        newnode->next = NULL;
        uTail = newnode;
      }
     }
    
    }
    
    
    //but how will i know that i am referring to the linked list SiteList?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Read text from a file into an array
    By ccwash in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2005, 03:19 PM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM