我正在编写一个程序,它将文本文件作为输入,生成单词的索引并在文件和屏幕中输出输出(索引)。我编码如下,试图发现问题或者至少缩小它,但我不能。如果有人可以帮助或发现语法或代码逻辑方面的问题,我会很高兴知道。
void main(int argc, char * argv[])
{
//clearscreen
clrscr();
//if arguments are less that default of the program
if (argc < 2)
{
cout << "You should've input 3 arguments." ;
return;
}
//opening the input file and defining a pointer which points to it as argv[1]
FILE *fPtr = fopen(argv[1], "r+");
//defining a 2D array to hold maximum to 200 words holding maximum to 20 characters
char words[200][20];
//initializing words 2D array with zero ASCII
for(int i = 0; i < 200; i++)
for(int j = 0; j < 20; j++)
words[i][j] = 255;
//defining an 2D whi array which is supposed to show how many times words are placed in which lines in a defined layout
//it holds maximum to 200 words holding maximum 100 sentences
int index[200][100];
//initializing the index 2D array with zero ASCII
for(i = 0; i < 200; i++)
for(j = 0; j < 100; j++)
index[i][j] = 0;
//this array of characters max to 2000 is supposed to hold each line which is gotten with fgets
char buff[2000];
//initializing the buff array with zero ASCII
for(i = 0; i < 2000; i++)
buff[i] = 0;
//the max of the words used in the source file is 200. but this valuable named as 'last' says how many words are used in this source file
//its initalized as no words is held
int last = 0;
//defining a pointer to char of punctuation mark characters. all ASCII codes expect for a - z, A - Z, 0 - 9 and newline
char *punc;
//initializing punctuation marks array
for(i = 0; i < 256; i++)
{
if(i == 10)
continue;
if(i >= '0' && i <= '9')
continue;
if(i >= 'a' && i <= 'z')
continue;
if(i >= 'A' && i <= 'Z')
continue;
char *string = (char *) &i;
strncat(punc, string, 1);
}
//how many lines is read from the source file
int lineread = 0;
//a word which the processes are done on that
char *word;
//one line read
while( fgets(buff, 1999, fPtr) != NULL)
{
//how many words is read from the source file in the current line
int wordread = 0;
word = strtok(buff, punc);
//one word read
while( word != NULL)
{
//sorting
int k = 0;
while(strcmp(word, words[k])> 0)
k++;
if(strcmp(word, words[k]))
{
for(int l = last; l >= 0; l--)
{
strcpy(words[l + 1],words[l]);
for(int o = 0; o <= lineread; o++)
index[l + 1][o] = index[l][o];
}
last++;
strcpy(words[k],word);
for(l = 0; l <= lineread; l++)
index[k][l] = 0;
}
index[k][lineread]++;
wordread++;//go to next word
word = strtok(NULL, punc);
}
lineread++;//go to next line
}
//closing the input file
fclose(fPtr);
//opening the output file and defining a pointer which points to it as argv[2]
FILE *fPtr2 = fopen(argv[2], "w+");
//showing the index in cmd
for(i = 0; i <= last; i++)
{
printf("%-20s" , words[i]);
fprinf(fPtr2, "%-20s" , words[i]);
int m = 0;
for(j = 0; j <= lineread; j++)
{
if(m)
{
printf("%c", ',');
fprintf(fPtr2, "%c", ',');
}
if(index[i][j])
{
printf("%i", j + 1);
fprintf(fPtr2, "%i" ,j + 1);
}
if(index[i][j] > 1)
{
printf("(%i)", index[i][j]);
fprintf(fPtr2, "(%i)", index[i][j]);
m = 1;
}
}
printf("\n");
fprintf(fPtr2, "\n");
}
//closing the output file
fclose(fPtr);
}
现在它出错,因为异常程序终止NULL指针赋值。我必须使用turbo c,我使用DOS SHELL。资源中有一个文件名为“input.txt”。在DOS SHELL我写这个:
programname.exe input.txt output.txt
如果input.txt是这样的话,和我想要的输出:
hello. hello.
how are
you? hello.
desired output:
hello 1(2),3 //2 times in line 2, 1 time in line 1
how 2 //1 time in line 2
are 2 //1 time in line 2
you 3 //1 time in line 3
答案 0 :(得分:0)
问题在于这一行:
while(strcmp(word, words[k])> 0)
因为index
被初始化为255
并且根据算法strcmp
的工作基于,对于第一个单词,代码正常工作并将其放入word[0]
但是对于第二个词它将无法正常工作。例如:
first word: Kittens
second word: Biscuits
首先strcmp
将Kittens
与255 ASCII code
进行比较。结果是Kitten
更小,并且流程会将其放在word[0]
中。但是对于第二个单词,strcmp
将Biscuits
与Kittens
进行比较,结果是Kittens
更大,程序将通过另一个word[]
块不想要。该程序将通过将word[]
初始化为0
并使用此代码而非首次提及的代码来修复:
while(strcmp(word, words[k])> 0)