Guest itmpm Posted February 11, 2003 Report Share Posted February 11, 2003 Hi. I have a wierd problem with my C program, the code of which is listed below. The program is supposed to generate strings according to certain critera. It goes through 1022 iterations, and then bombs in fgets after looking up the word "woe" (as far as I can tell), which is not near the end of the file. The gdb run looks like this: (gdb) run d Starting program: /home/matt/cryptorithm/crg d d Program received signal SIGSEGV, Segmentation fault. 0x4007e214 in fgets () from /lib/i686/libc.so.6 (gdb) up #1 0x08048674 in checkWord (candidate=0xbffff310 "wltrmcmht") at crg.c:87 87 fgets(readers, WORDSIZE-1, fp); (gdb) print readers $1 = "woedsein", '' <repeats 12 times>, "nismnux", '' <repeats 46 times> I know this is a curly one; any help would be greatly appreciated. Cheers, Matt ******* Program code follows ********** #include <stdio.h> #include <strings.h> #define ASIZE 10 #define W 0 #define T 1 #define E 2 #define R 3 #define H 4 #define C 5 #define A 6 #define L 7 #define M 8 #define STAR 9 #define BLANK -1 #define TRUE 1 #define FALSE 0 char letters[ASIZE] = { 'w','t','e','r','h','c','a','l','m', '*' }; int answer[9]; int digits[ASIZE]; int count=0; void solvum(int); char ch; main(int argc, char *argv[]) { char stupid; int i=0; for (; i<ASIZE;) digits[i++]=BLANK; if (argc > 1) { letters[9] = ch = argv[1][0]; fprintf(stderr, "%cn", ch); solvum(0); } else { for (ch='a'; ch<='z'; ch++) { printf("ch is %cn", ch); switch (ch) { case 'w': case 't': case 'e': case 'r': case 'h': case 'c': case 'a': case 'l': case 'm': break; default: fprintf(stderr, "%cn", ch); letters[9]=ch; solvum(0); break; } } } // cin >> stupid; } char equiv(int c) { int i; for (i=0; i<ASIZE; i++) if (digits==c) return letters; return 0; } #define WORDSIZE 80 int checkWord(char * candidate) { FILE *fp = fopen("/usr/share/dict/words", "r"); char readers[WORDSIZE]; int found, finded=7483; /* printf("candidate is %sn", candidate); */ fgets(readers, WORDSIZE-1, fp); while (!feof(fp)) { readers[strlen(readers)-1]=''; found=strcasecmp(readers, candidate); /* printf(">%s<, >%s<.n", readers, candidate); if (found !=finded) { printf("found is %dn", found); finded=found; } */ if (found==0) { close(fp); return TRUE; } else if (found>0) { close(fp); return FALSE; } fgets(readers, WORDSIZE-1, fp); } close(fp); return FALSE; } void solvum (int i) { int j, d; if (i < ASIZE) { for (d=0; d<10; d++) { for (j=0; j<i; j++) if (digits[j] == d) { break; } if (digits[j] != d) { digits = d; solvum(i+1); digits = BLANK; } } } else { int carry; char buffer[10]; char *result; answer[8] = (digits[W] + digits[T])%10; carry =(digits[W] + digits[T])/10; answer[7] = (carry + digits[E] + digits[R])%10; carry = (carry + digits[E] + digits[R])/10; answer[6] = (carry + digits[H] + digits[E])%10; carry = (carry + digits[H] + digits[E])/10; answer[5] = (carry + digits[T] + digits[H])%10; carry = (carry + digits[T] + digits[H])/10; answer[4] = (carry + digits[T] + digits[C])%10; carry = (carry + digits[T] + digits[C])/10; answer[3] = (carry + digits[A] + digits[L])%10; carry = (carry + digits[A] + digits[L])/10; answer[2] = (carry + digits[M] + digits[E])%10; carry = (carry + digits[M] + digits[E])/10; answer[1] = (carry + digits[M])%10; carry = (carry + digits[M])/10; answer[0] = carry; for (j=0; j<9; j++) buffer[j]=equiv(answer[j]); buffer[9]=''; result=answer[0]?&(buffer[1]):buffer; /* printf("%sn", result); */ printf("%dn", ++count); if (checkWord(result)) { printf("%c ", ch); for (j=0; j<ASIZE; j++) printf("%d", digits[j]); printf(" %sn", result); } } } Quote Link to comment Share on other sites More sharing options...
Cannonfodder Posted February 11, 2003 Report Share Posted February 11, 2003 Since you are using fopen() to open the file, you need to use fclose() to close the file. You are using close(). So basically, you are not closing the file and eventually reach the maximum number of files that can be opened. Check out this example: http://www.cplusplus.com/ref/cstdio/fgets.html Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.