Using fgets on a file

This is a discussion on Using fgets on a file within the C Programming forums, part of the General Programming Boards category; I am trying to write a program that will spell check a file by reading in two files. he problem ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    15

    Using fgets on a file

    I am trying to write a program that will spell check a file by reading in two files. he problem I am having is that I have to enter the file containing a list of dictionary words into a binary search tree. I am trying to use fget to get all the words in the file into the tree. When I try it the way it is posted below only the first word goes through the insert function. When I try to write while(!feof(ifp)) it goes through all the words, but each word enters the BST on its own thus creating its own BST. I don't know how to get the words from the dictionary file into the BST and have it create a whole BST.
    Last edited by MasterAchilles; 04-17-2008 at 10:33 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    We just did this, so you should be able to find it in a search (it might have been in the C++ forum; I don't recall). Anyway, the point is that you need to set the link of the parent down to the child, or else your word just floats in the ether.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    15
    Quote Originally Posted by tabstop View Post
    We just did this, so you should be able to find it in a search (it might have been in the C++ forum; I don't recall). Anyway, the point is that you need to set the link of the parent down to the child, or else your word just floats in the ether.
    Thanks. Sorry about that. I should have searched before I posted.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > (*root)->word = dictionary_words;
    Try using strcpy() to make a copy of the word, and see if that helps:
    strcpy((*root)->word, dictionary_words);

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    15
    For some reason that cased my function to crash. I don't know why because it makes more sense then just setting them equal to each other. I read the FAQ about feof and loops and see that I shouldn't use while(!feof(ifp)) and replaced it with what is recommended in the FAQ.
    Last edited by MasterAchilles; 04-18-2008 at 01:06 AM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Does your struct contain a char[] or a char*? If the latter, we're in for a lot more trouble, in that you have no space to store a word (a pointer just points somewhere, and in this case to nowhere special, and certainly not anywhere you could store something).

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    15
    I have no warnings or errors now. Now I think I should erase some of these printf functions. That last run took over 3 minutes to finish. It's still replacing the first word with the newest word entered. I removed \n from the words using strtok but I think it is still replacing the head node.
    Last edited by MasterAchilles; 04-18-2008 at 01:06 AM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't assign to an array. To copy the string in, you'll need to use strcpy.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I ran your latest code with a test file and it seemed like the nodes were inserted into the tree. I created a function to print the tree afterwards, and it looked like the words were properly inserted. I didn't run the spellcheck part of your code.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    http://cpwiki.sf.net/A_pointer_on_pointers
    Unfortunately, an array cannot be copied by assign. Instead, by design, the address to the first element is returned so you'll be dealing with pointers instead. And when dealing with pointers, it's good to read up about them from that article.
    This is why you can't simply a string - because you're going to make a copy of the pointer, which means the data itself won't be duplicated, only the address where the data is stored.

    It also sounds like your code has a lot of problems or complexity in it. It might just be a ticking time bomb. Perhaps you should try posting your code, if not already done so elsewhere so someone can have a look.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Firstly, you need to do something about those scanf reading strings.
    See http://cpwiki.sf.net/Buffer_overrun
    And the problem, I think, may be your last loop:

    Code:
    	while(!feof(fin))
    	{
    		fgets(spellcheckwords, 1024, fin);
    		   
    		if(feof(fin))
    		{
    			break;
    		}
    		
    		fixedlist = strtok(spellcheckwords, delimiters);
    		
    		while(fixedlist != NULL)
    		{
    			printf("\n%s\n", fixedlist);
    			fixedlist = strtok(NULL, delimiters);
    		}
    	  }
    Consider that fgets might read the last line in your file and encounters the end of file. The EOF flag gets set and the line isn't processed. You did it right before. Put the fgets as the loop condition. Fgets will return 0 if it didn't read anything.
    Last edited by Elysia; 04-18-2008 at 04:56 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		if(feof(ifp))
    		{
    			break;
    		}
    This is not needed if you check that fgets() returns not-NULL. If you have specific circumstances[1], the feof() will be true even if you managed to read something from the file, and that's obviously not particularly useful.

    [1] Particularly if the file ends without a newline.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    15
    Quote Originally Posted by Elysia View Post
    Firstly, you need to do something about those scanf reading strings.
    See http://cpwiki.sf.net/Buffer_overruns
    And the problem, I think, may be your last loop:

    Consider that fgets might read the last line in your file and encounters the end of file. The EOF flag gets set and the line isn't processed. You did it right before. Put the fgets as the loop condition. Fgets will return 0 if it didn't read anything.
    Thanks. I'll fix that right now. Thanks a lot for readings its helping me understand these concepts better. I updated my code and the scanf, but using fgets its not working. Do I have to do it a different way since I am reading a file? I tried sizeof(), but that didn't fix the problem either.
    Last edited by MasterAchilles; 04-18-2008 at 07:08 AM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by MasterAchilles View Post
    Thanks a lot for readings its helping me understand these concepts better.
    No problem about those. That's what they're for.
    Oops, typo in link >_< Fixed now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    15
    Quote Originally Posted by matsp View Post
    Code:
    		if(feof(ifp))
    		{
    			break;
    		}
    This is not needed if you check that fgets() returns not-NULL. If you have specific circumstances[1], the feof() will be true even if you managed to read something from the file, and that's obviously not particularly useful.

    [1] Particularly if the file ends without a newline.

    --
    Mats
    I see. That was a remnant from before I switched from while(!feof(ifp)). I had no idea it could have such an effect. Thanks a lot for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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