| | |
Infinite loop problem
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 15
Reputation:
Solved Threads: 1
First here is my header file
here is the source code itself
the error
I believe the problem is this
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?
c Syntax (Toggle Plain Text)
/* @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
c Syntax (Toggle Plain Text)
/* @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
C Syntax (Toggle Plain Text)
$ ./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
c Syntax (Toggle Plain Text)
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?
•
•
•
•
Test for an end of file condition.
Use something like while(!feof(inputfile))
•
•
•
•
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.
while(1) will not stop if nothing tells it to stop. C Syntax (Toggle Plain Text)
while( fgets(line, MAXLENGTH, inputfile) ) { printf("HELP I'M IN A WHILE LOOP.\n"); buildTrie(nodeStart, inputfile); }
![]() |
Similar Threads
- Input Stream Problem (C++)
- Loop problem (C++)
- XP Startup Problem: Infinite Loop (Windows NT / 2000 / XP)
- Strange Excel infinite-loop problem (Windows Software)
- strange loop problem (Visual Basic 4 / 5 / 6)
- Help Me Solve My Infinite Loop (C++)
- infinite loop... (C++)
Other Threads in the C Forum
- Previous Thread: Need help using postest loop in C, Please Help
- Next Thread: Newbie question in C PLEASE help!!!
| Thread Tools | Search this Thread |
#include adobe ansi api append array arrays asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework frequency function getlasterror givemetehcodez global grade gtkgcurlcompiling hacking hardware highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab windows.h windowsapi






