First here is my header file

/* @source Simplespell Program
 * 
 * @author: Chris King(cpking@ncsu.edu)
 * @@
 * 
 * node.h
 * CSC 230 Fall 2008
 *
 * The @ signs along with the word denote specific information about
 * the  program.  Using the standards described in:
 * http://emboss.sourceforge.net/developers/c-standards_new.html
 */

typedef struct node{ //just creating a node
	struct node *array[27]; // a node is merely an array of pointers... TO MORE POINTERS!
}Node, *node_ptr; // wrapping this up

node_ptr createRoot(void); // method to create a root node.

node_ptr addNode(char); //method to add a node to a trie

node_ptr createNode(void); // method to generate a new node

node_ptr buildTrie(node_ptr, char*); // eventually this will compile the trie

int readFile(FILE *);

here is the source code itself

/* @source Simplespell Program
 * 
 * @author: Chris King(cpking@ncsu.edu)
 * @@
 * 
 * spellback.c
 * CSC 230 Fall 2008
 *
 * The @ signs along with the word denote specific information about
 * the  program.  Using the standards described in:
 * http://emboss.sourceforge.net/developers/c-standards_new.html
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include "node.h"

#define MAXSIZE 2048 // max size of a char array here (yes i know it could have been 101)
#define MAXLENGTH 100

struct node *rootNode; // our top node in chief.
FILE *inputfile; // any input file here

int main(int argc, char *argv[]){
	int ck;
	ck = argc;
	createRoot();
	//printf("%d\n", ck);
	
	FILE *fp;
	
	if(ck == 1){ // checking for no parameters here
		printf("Opening default file\n");
		readFile(fp = fopen(".WORDS", "r"));
		
		fclose(fp);
		printf("Closed default file\n");
		
	}
	
	if(ck == 2 || ck == 4){ // checks for bs parameters here
		printf("Invalid Input\n");
		return -1;
	}
	
	if(ck == 3){ // checks for 1 input file here
		printf("Opening default file\n");
		fp = fopen(".WORDS", "r");
		fclose(fp);
		//point fp to next file
		printf("Closed default file\n");
		fp = fopen(argv[2], "r");
		printf("Opening selected file\n");
		fclose(fp);
		printf("Closed selected file\n");
	}
	
	if(ck >= 5){ // checks for more than 5 (this is not finished yet)
		printf("Opening default file\n");
		fp = fopen(".WORDS", "r");
		fclose(fp);
		printf("Sorry the rest has not been written yet.\n");
	}
	
	return 0;
}

node_ptr createRoot(void){ // this simply uses calloc to allocate the ram for my root trie node
	rootNode = calloc(1,sizeof(Node));
	return rootNode;
}


node_ptr createNode(void){ // here we find new nodes being created for a trie
	struct node *newNode;
	newNode = calloc(1,sizeof(Node));
	return newNode;
}

int readFile(FILE *inputfile){
	char line [MAXLENGTH];
	struct node *nodeStart;
	nodeStart = rootNode;
	printf("I AM BEFORE THE WHILE LOOP.\n");
	while(1){
		printf("HELP I'M IN A WHILE LOOP.\n");
		buildTrie(nodeStart, fgets(line, MAXLENGTH, inputfile));
	}
	return 0;
}

node_ptr buildTrie(node_ptr nodeIn, char *text){
	while(text[0] == '\0'){
		break;
	}
	
	//rest of code here
	printf("we have a line.\n");
	return rootNode;
	
}

the error

$ ./simplespell 
Opening default file
I AM BEFORE THE WHILE LOOP.
HELP I'M IN A WHILE LOOP.
we have a line.
HELP I'M IN A WHILE LOOP.
we have a line.
HELP I'M IN A WHILE LOOP.
we have a line.
HELP I'M IN A WHILE LOOP.
we have a line.
HELP I'M IN A WHILE LOOP.
we have a line.
HELP I'M IN A WHILE LOOP.
we have a line.
HELP I'M IN A WHILE LOOP.
CONTINUES FROM HERE FOREVER

I believe the problem is this

while(1){
		printf("HELP I'M IN A WHILE LOOP.\n");
		buildTrie(nodeStart, fgets(line, MAXLENGTH, inputfile));
	}
	return 0;

I am trying to get lines until the end of the file, and pass the char array via pointer into the buildTrie function. How can I improve this?

Recommended Answers

All 8 Replies

Why do you put buildTrie in an endless while loop???
while(1) will loop forever !!

Well i need to run fgets to get the pointer to the char array, i need to run this until the end of file... how would that while loop look like? Yes I am aware of while true... but I assumed if there was an error it would just break.

Test for an end of file condition.
Use something like while(!feof(inputfile))

Test for an end of file condition.
Use something like while(!feof(inputfile))

Bad idea, feof() shouldn't be use as the exit control of a loop. Read on.

OK Aia I know fgets gets NULL when getting something past eof, but I was concentrating on the infinity of the while loop first.
But anyway, thanks for reminding me!

Well i need to run fgets to get the pointer to the char array, i need to run this until the end of file... how would that while loop look like? Yes I am aware of while true... but I assumed if there was an error it would just break.

Hopefully, your assumption has been adjusted to go more in accordance with reality. As you have seen while(1) will not stop if nothing tells it to stop.

while( fgets(line, MAXLENGTH, inputfile) ) {
    printf("HELP I'M IN A WHILE LOOP.\n");
    buildTrie(nodeStart, inputfile);
}

Might work.

i have also problem in loop,do loop and while loop.can some suggest me about this three loops ?

i have also problem in loop,do loop and while loop.can some suggest me about this three loops ?

You'll receive a better treatment if you create your own thread, post the pertinent portion of the code you are having problems with, and last but not least, you read, get aware, and follow the rules of the forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.