我目前正在嘗試編寫一個應用程序來計算ASCII文件中的單詞出現次數(標點符號被剝離並忽略空格)。應用程序應該將單詞和單詞計數存儲在數據結構中,該數據結構最終將按降序排序,然後打印到CSV文件。字數應用程序 - C
我已經開始了這個程序,但是當我嘗試保存一個新單詞時,我遇到了分段錯誤。這裏是我的代碼(我知道,這不是一個完美的執行,我不打算煉化吧):
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#define TRUE 1
#define FALSE 0
/* This program is designed to take an ASCII input file, count the occurrences of words in it
* and write an output file displaying the data. I intend for it to convert uppercase to
* lowercase, so as not to generate duplicate words in the data structure. It should also
* ignore whitespace and punctuation.
*/
void getWords(void);
void printFile(void);
void save(char *input);
struct word {
char *str;
int wc;
};
struct word *warray = NULL;
FILE *infile;
FILE *outfile;
void getWords(void)
{
rewind(infile);
char cw[100]; // Current word storage
int i = 0, j = 0, c;
while((c = fgetc(infile)) != EOF)
{
if(isalpha(c))
{
if(isupper(c))
{
cw[i] = tolower(c);
++i;
}
else
{
cw[i] = c;
++i;
}
}
else
{
if(c == '\n' || c == '\t' || c == ' ')
{
cw[i] = '\0';
i = 0;
save(cw);
for(j = 0; j < cw[99]; j++)
{
printf("%c", cw[j]);
}
}
}
}
}
void printFile(void)
{
int i, c;
printf("Printing the file to be counted in lowercase...\n");
for(i = 0; (c = fgetc(infile)) != EOF; i++)
{
if(ispunct(c) || isdigit(c))
{
++i;
}
else
{
putchar(tolower(c));
}
}
}
void save(char *input)
{
int exists = FALSE, i = 0;
int elements = sizeof(warray)/sizeof(struct word);
if(!warray)
{
warray = malloc(sizeof(struct word));
printf("Made array.\n");
}
else
{
printf("New.\n");
warray = realloc(warray, (elements++)*sizeof(struct word));
}
while(i < elements)
{
printf("in while loop\n");
if(strcmp(input, warray[i].str) == 0)
{
warray[i].wc++;
}
else
{
++i;
}
}
printf("Out while loop\n");
if(strcmp(input, warray[i].str) == 1)
{
printf("Inside save if statement\n");
warray[elements].str = malloc(strlen(input)+1);
strcpy(warray[elements].str, input);
warray[elements].wc = 1;
elements++;
}
}
int main (int argc, char *argv[])
{
if (argc < 3)
{
puts("Please supply the input filename and desired output filename as arguments.");
return 1;
}
infile = fopen(argv[1], "r");
if(infile == NULL)
{
printf("File failed to open. Error: %d\n", errno);
return 1;
}
else
{
puts("File opened successfully.");
printFile();
getWords();
}
return 0;
}
我已經把一些打印語句,試圖找出問題,並似乎運行到這裏的問題時,save(char *input)
函數內部:
if(strcmp(input, warray[i].str) == 1)
{
printf("Inside save if statement\n");
warray[elements].str = malloc(strlen(input)+1);
strcpy(warray[elements].str, input);
warray[elements].wc = 1;
elements++;
}
我確實有一種感覺,那是因爲我問STRCMP檢查,如果它的價值== 1,當或許我應該只檢查對於任何非零值,但我已經嘗試過,我仍然遇到分段錯誤。
我會很感激,如果任何人都可以指出我在正確的方向,並提前致謝!
首先要做的是:使用調試器並確定哪條線路導致錯誤。檢查變量並試圖弄清楚它們如何發揮它們的價值。如果需要,請逐步重新運行,觀察每個步驟的變量。如果不確定如何執行上述任何操作,請提出有關這些操作的問題。 –