Thread: Reading from file strange problem

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    Reading from file strange problem

    Program has to read from a file of 10000 names in form of Surname Name(which are seperated by space or tab,fill with them a simple-BST ,print them in order and then search from a file of 1000 names in the BST.
    The problem:I should find 900 people after searching.I found 825.
    The reason:Some names are saved in BST with some characters that not exist. e.g. We read from file Wu Teresa and this name is saved as Wuax Teresa and Fox Jim as Fox' Jim.
    I run at a linux environment.Bellow is my code,sorry for greeklish but i had write in greeklish.Here is the part that i read from stream and save into BST.Reading from search file is done with same way.
    Code:
    while ( fgets(buf, LINE_LENGTH, fp) != NULL) 
    	{
            s=buf;
            empty_line=0;
    	size_for_malloc=0;
    	until=strchr(s,'\t' ); /* until points to 'tab'*/
    	if(until == NULL)
    	{ /* Files from uliko-3hs-askhshs may have an empty line at the end */
    		    until=strchr(s,' '); /* In case names are seperated by whitespace*/
    		    if(until == NULL)
    		    {
    				empty_line=1;
    				until=strchr(s,'\n'); /* Case of empty line */
    			}
    			if(until == NULL)
    			{
    				printf("Sorry,file has to be in form Surname Name\n");
    				return -1;
    			}
    		}
    		if(empty_line) continue;
    		while(strcmp(s,until)!=0)/* Go until the end of surname */
    		{
    			size_for_malloc++;
    			s++;
    		}
    		counter=0;
    		while( (*s==' ') || (*s=='\t') )/* Bypass spaces */
    		{
                            size_for_malloc++;
    			s++;
    			counter++;
    		}
    		size_for_malloc-=counter;
    		counter=1;
    		line.surname=malloc( (size_for_malloc+1)*sizeof(char) );
    		strncpy(line.surname,buf,size_for_malloc);
    		size_for_malloc=0;
    		until=strchr(s,' ' );
    		if(until == NULL)
    		{
                            until=strchr(s,'\t' );
    			if(until == NULL)
    			{
                                   until=strchr(s,'\n' );
    			}
    		}
      		while(strcmp(s,until)!=0)
    		{
    			size_for_malloc++;
    			s++;
    		}
    		s-=size_for_malloc;
    		line.name=malloc( (size_for_malloc+1)*sizeof(char) );
    		strncpy(line.name,s,size_for_malloc);
    		count_lines++;
                    Tree_eisagogi(&BulkTree , line , &error);/* Insert name to BST */
                    if(error)
                    {
    			printf("Sorry,can not allocate memory . Read %u lines\n",count_lines);
    			return -1;
    		}
    	}
    Thanks in advance

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Show me a few lines of the input file... preserve all characters and spacing in code tags, please.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    input file
    Code:
    Stevenson       Christy
    Riddle  Melissa
    York    Joe
    Shapiro Calvin
    Bynum   Gene
    Atkinson        Dan
    Andrews Juan
    Wilkerson       Francis
    Hubbard Sidney
    Brooks  Billy
    Hansen  Shirley
    Bauer   Kent
    Snow    Gretchen
    Patton  Jacqueline
    Chen    Anna
    Peele   Lester
    Thomson Scott
    Bernstein       Maxine
    Griffith        Phyllis
    search file
    Code:
    Black   Constance
    Fox     Sarah
    Hatcher Gladys
    Wu      Hazel
    Lloyd   Hazel
    Joyce   Jerome
    Welch   Vincent
    Matthews        Kim
    Chappell        Joseph
    MacDonald       Alison
    Kane    Eric
    Butler  Heather
    Pickett Claire
    Bowman  Michele
    Barton  Terry

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't see you putting \0 at the end of any of your names.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... and I gather from your code you're loading these into some kind of struct...
    Code:
    struct name
      { char first[25];
         char last[25]; };
    sort of thing...

    You may want to look into the fscanf() function here... it basically does pattern matching on your file and loads variables that match the pattern...

    For example...
    Code:
    // create the array (use malloc so you can realloc if necessary)
    struct name *names = malloc(10000 * sizeof[name]);
    int x = 0;   // array pointer
    
    // open your file
    
    // read the file
    while (! feof(file))
      { if (fscanf (" %s %s",names[x]->last,names[x]->first))
          x++; }
    Now this is untested and for concept only... but something along these lines will probably save you a lot of hassle, especially if you get blank lines, random numbers of spaces or multiple tabs...

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by tabstop View Post
    I don't see you putting \0 at the end of any of your names.
    Doesn't fgets put it automatically?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by std10093 View Post
    Doesn't fgets put it automatically?
    strncpy doesn't.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( fgets( ... ) )
    {
        if( sscanf( buf, "%s %s", word1, word2 ) == 2 )
        {
            sprintf( buf, "%s %s", word1, word2 );
            malloc_len = 1 + strlen( buf );
            ... malloc
            ... strcpy( mallocedplace, buf );
        }
        else /* didn't get what you expect, quit maybe? */
            ...
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by tabstop View Post
    strncpy doesn't.
    Yes,you are right.But why does this cause a problem?Names are read the same way from input file and from for-search file.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by std10093 View Post
    Yes,you are right.But why does this cause a problem?Names are read the same way from input file and from for-search file.
    The first time through, you read in "fred", and you strncpy "fred" to a buffer. The next time you read "bob", and you strncpy "bob" to that same buffer. That buffer now looks like: bobd.
    Quote Originally Posted by man strncpy
    The strncpy() function copies at most len characters from src into dst.
    If src is less than len characters long, the remainder of dst is filled
    with `\0' characters. Otherwise, dst is not terminated.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by std10093 View Post
    Yes,you are right.But why does this cause a problem?Names are read the same way from input file and from for-search file.
    If you don't create the tree correctly in the first place, then searching for something later is irrelevant because the names don't actually exist in the tree.

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by quzah View Post
    The first time through, you read in "fred", and you strncpy "fred" to a buffer. The next time you read "bob", and you strncpy "bob" to that same buffer. That buffer now looks like: bobd.


    Quzah.
    YES!! that's it! This is what i thought it was the problem but a cannot find the solution.I tried to fill the buffer whit spaces before the 'reading' for the next name but this did not work.So the solution is to put a '\0' at the end of name?

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    However this does not explain the existance of some characters that do not exist in names.Such us
    Code:
    ',Ψ,π

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by std10093 View Post
    However this does not explain the existance of some characters that do not exist in names.Such us
    Code:
    ',Ψ,π
    Sure it does.
    Code:
    int main( void ) {
        char foo[BUFSIZ];
        return 0;
    }
    What values do the characters in foo hold?


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Pseudo-random.Right? So if i figure out a way to put '\0' at the end of every name and surname then the program will be correct!Maybe i could use strcpy instead of strncpy-in case i put the '\0' .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bus error reading file; strange i value
    By xenonscreams in forum C Programming
    Replies: 3
    Last Post: 09-17-2010, 10:45 AM
  2. strange file opening problem
    By ac251404 in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2006, 04:35 PM
  3. Problem reading from a file..
    By Candelier in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 12:42 AM
  4. Strange file problem.
    By tilex in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2004, 12:32 AM
  5. A strange problem with creating a file
    By Waldo2k2 in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2002, 03:42 PM