该程序的重点是取2个文件,一个字典,另一个是文本,创建一个输出文件,并将文本文件中的所有单词放入其中,但将字典中不存在的单词大写。
当我运行程序时,它一直在询问输入,所以我似乎陷入了 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));
}