Why does the program terminate at "fscanf" function?

This is a discussion on Why does the program terminate at "fscanf" function? within the C Programming forums, part of the General Programming Boards category; Please notice the "hashBuild" function. I sure that the wrong message has being occured at "fscanf". But I don't know ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    Why does the program terminate at "fscanf" function?

    Please notice the "hashBuild" function. I sure that the wrong message has being occured at "fscanf". But I don't know how to correct it. Please help...
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <assert.h>
    #define true 1
    #define false 0
    #define bool short
    #define HASHTSIZE 80000
    #define nameLen 100
    
    char hashT[HASHTSIZE][nameLen];
    int hashTC[HASHTSIZE];
    int hashIdx[HASHTSIZE];
    int idxTail;
    
    void init()
    {
    	int i;
    	for (i = 0; i < HASHTSIZE; i++) {
    		hashT[i][0] = '\0';
    		hashTC[i] = 0;
    		hashIdx[i] = 0;
    	}
    	idxTail = 0;
    }
    
    int charType(char c)
    {
    	/* a = 0, z = 25	*/
    	return ((int)c-97);	
    }
    
    void pasteString(int hashTIndex, char *charMatch)
    {
    	int cmLen = strlen(charMatch);
    	int i;
    	for (i = 0; i < cmLen; i++)
    		hashT[hashTIndex][i] = charMatch[i];
    }
    
    bool sameString(char *a, char *b)
    {
    	int alen, blen, i;
    	alen = strlen(a);
    	blen = strlen(b);
    	if (alen == blen) {
    		for (i = 0; i < alen; i++)
    			if (a[i] != b[i])
    				return false;
    		return true;
    	}
    	return false;
    }
    
    void hashCount(char *charMatch)
    {
    	int i;
    	bool addOne = true;
    	i = charType(charMatch[0]);
    	while (!sameString(&(*hashT[i]), charMatch) && i < HASHTSIZE) {
    		if (hashT[i][0] == '\0') {
    			pasteString(i, charMatch);
    			hashTC[i] = 1;
    			hashIdx[idxTail] = i;
    			idxTail++;
    			addOne = false;
    			break;
    		}
    		i = i + 26;
    	}
    	if (addOne && i < HASHTSIZE)
    		hashTC[i]++;
    	//	printf("hashTC[%d] = %d\n", i, hashTC[i]);
    }
    
    int hashFind(char *charMatch)
    {
    	int i;
    	i = charType(charMatch[0]);
    	while (!sameString(&(*hashT[i]), charMatch) && i < HASHTSIZE) {
    		if (hashT[i][0] == '\0')
    			return -1;
    		i = i + 26;
    	}
    	return i;
    }
    
    void hashBuild()
    {
    	FILE *fr;
    	char *name;
    	fr = fopen("namebase.dat", "rt");
    	assert(fr != NULL);
    	/*
    	while (fscanf(fr, "%s", name) != EOF)
    		hashCount(name);
    	fclose(fr);
    	*/
    }
    
    void printHashC()
    {
    	FILE *fw;
    	int i;
    	hashBuild();
    	fw = fopen("namebase.out", "wt");
    	assert(fw != NULL);
    	for (i = 0, --idxTail; i <= idxTail; i++) {
    		fprintf(fw, "%s %d\n", hashT[hashIdx[i]], hashTC[hashIdx[i]]);
    	}
    	fclose(fw);
    }
    
    int main()
    {
    	init();
    	printHashC();
    	return 0;
    }
    The bugs occured at comment scope.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Hmm. Perhaps look into this lint first.
    Code:
    void hashCount(char *charMatch)
    {
    	int i;
    	bool addOne = true;
    	i = charType(charMatch[0]);
    	while (!sameString(&(*hashT[i]), charMatch) && i < HASHTSIZE) {
    /* Possible creation of out-of-bounds pointer (26 beyond end of data) by operator '['*/
    		if (hashT[i][0] == '\0') {
    			pasteString(i, charMatch);
    			hashTC[i] = 1;
    			hashIdx[idxTail] = i;
    			idxTail++;
    			addOne = false;
    			break;
    		}
    		i = i + 26;
    	}
    	if (addOne && i < HASHTSIZE)
    		hashTC[i]++;
    	//	printf("hashTC[%d] = %d\n", i, hashTC[i]);
    }
    
    int hashFind(char *charMatch)
    {
    	int i;
    	i = charType(charMatch[0]);
    	while (!sameString(&(*hashT[i]), charMatch) && i < HASHTSIZE) {
    /* Possible creation of out-of-bounds pointer (26 beyond end of data) by operator '['*/
    		if (hashT[i][0] == '\0')
    			return -1;
    		i = i + 26;
    	}
    	return i;
    }
    [edit]D'oh. And of course allocate memory into which the string can go.
    Code:
    void hashBuild()
    {
    	FILE *fr;
    	char *name;
    	fr = fopen("namebase.dat", "rt");
    	assert(fr != NULL);
    	/*
    	while (fscanf(fr, "%s", name) != EOF)
    		hashCount(name);
    	fclose(fr);
    	*/
    }
    Last edited by Dave_Sinkula; 05-22-2006 at 11:10 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    I can't say for what Dave_Sinkula said, because I've never
    studied hashing but I think the runtime error is due to "rt" - never
    heard of that open mode, probably meant it to be "r+"
    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

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    Thank you very much! sorry for my stupid mistake..... forget to allocate *name......
    The bound case, I think it's easy to handle. efficiently created function to be boundCaseControl(int *hashTable).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. one function in my program won't work....
    By talz13 in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2003, 12:19 PM
  3. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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