File I/O problem

This is a discussion on File I/O problem within the C Programming forums, part of the General Programming Boards category; I'm trying to read a text file in C and insert all of the words in the file to a ...

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

    File I/O problem

    I'm trying to read a text file in C and insert all of the words in the file to a linked list which will then later be traversed.

    I've coded my program to take a file specified at the command line, open it and read each word one by one inserting it into the list. However on compiling and running the program it seems to go into an infinate loop, taking up a lot of processing power and writing lots to the disk (on both windows and linux systems).

    If anyone can help, that'd be great.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct L {char *word; struct L *next;} List;
    
    List *insertlist(char *head, List *tail) {
    	List *t = calloc(1, sizeof(List));
    	t->word = head;
    	t->next = tail;
    	return t;
    }
    
    int *makelist(FILE *fp) {
    	List *wordlist;
    	char *word;
    	while (feof(fp) == 0) {
    		fscanf(fp, "&d", &word);
    		insertlist(word, wordlist);
    	}	
    	return 0;
    }
    
    int main (int argc, char *argv[]) {
    	FILE *fp;
    	if (fopen(argv[1], "r") != 0) {
    		fp = fopen(argv[1], "r");
    		makelist(fp);
    		fclose(fp);
    	} else {
    		printf("No such file\n");
    	}
    	return 0;
    }
    thanks in advance.

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by dacbo
    Code:
    List *insertlist(char *head, List *tail) {
    	List *t = calloc(1, sizeof(List));
    	t->word = head;
    	t->next = tail;
    	return t;
    }
    
    int *makelist(FILE *fp) {
    	List *wordlist;
    	char *word;
    	while (feof(fp) == 0) {
    		fscanf(fp, "&d", &word);
    		insertlist(word, wordlist);
    	}	
    	return 0;
    }
    I see several problems with this code. I won't go in to how you're reading words (or defining words, for that matter) in your text, but just how you're processing the data.

    In the function makelist(), you define the variable "word" as a pointer to char. This does not give you any storage for the text of the word, just a pointer to where it might be (though you're not setting it to point at anything.) When you do your fscanf(), you're using a bogus format -- I assume you meant to type "%d" rather than "&d", but even using "%d", which will read a decimal integer, you're not going to get text out of it. Further, you're passing a pointer to the char pointer called "word" to fscanf(), which means that the result of the fscanf() will be stored as the value of the pointer, which is not what you want.

    Moving on to the function insertlist(), you have a few problems -- your call to calloc() is a bit odd because you're putting the count where most programmers put the size of the item to be allocated, and the size of the item to be allocated where most programmers put the number of items to allocate; however this should still work. Further, you're not allocating any storage for the text of the word that you're trying to put into the list. Just assigning a pointer will not (in this case) give you the word text.

    Once you've addressed these problems, you should look closely at the program logic; walk through it step by step on paper and see if it does what you think it does. If you're still having problems after you've done that, then seek additional help.
    Insert obnoxious but pithy remark here

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    before doing thing with linked list u need to know how to read a file and fetech the data from file. the code below

    Code:
    fscanf(fp, "&d", &word);
    
    should be 
    
    fscanf(fp, "%s", word);
    its not '&' there its % its the format specifier and the u dont nned to give the address of the string as the string name itself is an address to starting location of the string

    2. need to update the wordlist pointer
    Code:
    wordlist = insertlist(word, wordlist);
    3. need to initalize the workdlist pointer before u call the insert fucntion
    Code:
    List *wordlist = NULL;

    ssharish2005

  4. #4
    old man
    Join Date
    Dec 2005
    Posts
    90
    Also, why call fopen() twice? Usually it's done something like this:
    Code:
      FILE *fp;
      fp = fopen ("whatever", "r");
      if (fp == NULL)
        perror ("whatever");
    As for the rest, you need to decide what you're storing. Probably you want to read the file into a buffer and store pointers to the words in your llist.

    I'd personally not use fscanf(), either; I'd filter the file myself with fgetc() ... but there's more than one way to do it, I suppose ...

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    thanks for your help guys

    stupid mistakes on those typos

    have since rewritten using fgetc()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 09:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04:32 PM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 01:14 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

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