該程序的要點是採取2個文件,一個字典,另一個是文本,創建一個輸出文件,並把它放在文本文件中的所有單詞,但大寫的單詞不存在於字典中。卡在fscanf循環
當我運行程序時,它一直要求輸入,所以它似乎卡在fscanf循環中。而我只有3個fscanf循環。它必須是其中之一,但我無法弄清楚是哪一個以及爲什麼。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#define wosi 20
int comp (const void *a, const void *b);
int main(int ac, char *av[])
{
int wordcount,i;
FILE *infi, *outfi;
char nothing, *dicptr, fina[100],letter[wosi];
unsigned char c;
/*to return error in case number of arguments mismatch*/
if (ac!=3)
{
fprintf (stderr," prog3: Man, I need 3 arguments to work!\n");
return (1);
}
/*first fscanf loop*/
while ((fscanf(infi,"%s",¬hing)!=0))
wordcount++;
/* end of step 2 */
dicptr = malloc(wordcount * wosi);
/* end of step 3*/
rewind(infi);
/*second fscanf loop */
for (i=0; fscanf(infi,"%s",&dicptr[i*wosi]) ;i++){}
/* this is qsort stage (finishing step 4) */
qsort (dicptr,wordcount,wosi,comp);
/*step 5 */
fclose (infi);
infi = fopen(av[2],"r");
if (infi == NULL)
{
perror("opening");
fprintf(stderr,"Can't open %s, the file is empty\n",av[2]);
return(1);
}
/*step 6 here */
strcpy(fina, av[2]);
strcat(fina, ".out");
outfi = fopen(fina, "w");
/*step 7*/
/* third fscanf loop */
while((fscanf(infi, "%s", letter)!= EOF));
{
for(i=0; letter[i]!='\0' ;i++)
{
c=letter[i];
letter[i]= toupper(c);
}
if(bsearch(letter,dicptr,wordcount,wosi,comp))
{
for(i=0;letter[i]!='\0';i++)
{
c=letter[i];
letter[i]= tolower(c);
}
}
/* fputs to print in out file*/
for(i=0; letter[i];i++)
{
fprintf(outfi,"%s",letter);
}
}
free(dicptr);
return (0);
}
int comp (const void *a, const void *b)
{
return (strcmp((const char *) a, (const char*) b));
}
http://stackoverflow.com/q/15440488/1758762 – duDE 2013-03-15 20:54:02
任何你可以計算出每個循環之前和之後打印標記到stderr的循環的機會? – 2013-03-15 20:54:19
我試過把打印標記和問題仍然是相同的。 – 2013-03-15 21:06:15