【问题标题】:Storing elements in structure array of words in C将元素存储在C中的单词结构数组中
【发布时间】:2015-01-11 22:16:16
【问题描述】:

当我尝试递增以在单词结构数组中存储另一个单词时,我有一个关于我的程序崩溃的问题。该程序会正确读取每一行和单词,但是由于某种原因,当我尝试递增以存储更多单词时,它总是会崩溃。有什么建议吗?

文本文件(输入):

OK UH UH SOUND FIELD IS UP 
OK RUNNING TEST SERIES 
OK 
SOUTHWEST SOUTHWEST CONFIRMED 
PEEN HIT CONFIRMED ARMED FIRE 
AND THAT'S TWO UM WHAT YOU WANT TO MOVE A LITTLE CLOSER 
OK 
OK 
PEEN FORE CONFIRMED ARMED 
ARMED FIRE 
THAT'S A THREE ARMED FIRE 
OH THAT'S ONLY A TWO OK 
GOING TO LASER CANNON 
BOX THAN OK I'M GOING TO GO FOR BAD CHOP CAN YOU CONFIRM BAD CHOP 
FIRE 
UH HOLD ON ARMED FIRE 
OK 
GO NEED ARMED FIRE 
THAT WAS A REPEAT HE'S MOVING OUT 
YEP YOU WANT TO GO AHEAD OF HIM 
OK 
YOU WANT TO DO A SWEEP 
SUE BID ARMED 
FIRE 
OK 
DEED NEED I'M GOING TO OK I'M GOING TO GO FOR SUE ZOO ARMED 
FIRE 
OK I'M GOING TO GO FOR DEED YEN 
DEED YEN DEED YEN 
FIRE 
OK I NEED A SWEEP 

结构代码:

typedef struct {
  char word[maxLetters];
  int  freq;
} WordArray; //struct type

主代码:

#define maxLetters 101
#define maxWords 200
int inputReader(WordArray input[], char f[maxWords]){

// ATTRIBUTES //
int i = 0;
int uniqueWords = 0;
int contains = 0;
int numOfWords = 0;
int k = 0;

FILE *finput;
char lineOfWords[1000];

finput = fopen( f, "r");



WordArray allWords[maxWords];


while(fgets(lineOfWords, maxWords, finput) != NULL) { // reads each line of words

     // PROBLEM IS HERE
     while(fscanf(finput, "%s", allWords[numOfWords].word) == 1) // reads each word from line
     { 

        printf("%s\n", allWords[numOfWords].word);// this works
        //numOfWords++;    if i add this i receive the error 
                          // "Segmentation Error(core dumped)"


     }
}

return 0; // default ignore this
}

【问题讨论】:

  • 表达式sizeof(input) 不会像你期望的那样工作,因为数组衰减到指针你没有得到你传递给函数的数组的大小,你得到的是指针的大小(在大多数 32 位系统上为4,在大多数 64 位系统上为8)。如果您需要函数知道数组的大小(以字节为单位),则需要将其显式传递给函数。
  • 感谢您指出这一点,但这与我的问题无关,因为我此时没有使用该变量
  • memset(lineOfWords, 0x00, sizeof(lineOfWords)); 并不是必需的。
  • 澄清 iharob 想说的是:您已经阅读了整行,无需再次从文件中读取单词,您只需将它们读入字符串,从该字符串中获取单词.
  • 如果你得到你所说的段错误,只需添加一个简单的 printf 将打印numOfWords 的值并确保它不会超过200,如果是,你会知道它为什么会崩溃。 编辑:简单地说:添加代码以检查边界并在尝试超出边界时打印错误消息。

标签: c file structure text-files


【解决方案1】:

你的程序崩溃了,因为你没有限制阅读的字数,你应该添加到外循环,

(numOfWords < maxWords)

以及我在源代码中添加了 cmets 的其他一些问题,我还修复了使用 strtok 解析部分的文件的双重读取。

int inputReader(WordArray input[], char f[maxWords])
{
    (void) input;
    // ATTRIBUTES //
    int numOfWords = 0;

    FILE *finput;
    char lineOfWords[1000];

    finput = fopen( f, "r");

    WordArray allWords[maxWords];

    /*
     * tell fgets NOT to read more than sizeof(lineOfWords) bytes, 
     * not maxWords, they are unrelated
     */
    while ((fgets(lineOfWords, sizeof(lineOfWords), finput) != NULL) && (numOfWords < maxWords)) { // reads each line of words
        char *pointer;
        char *token;

        pointer = lineOfWords;
        /* also prevent overflowing the struct array */
        while (((token = strtok(pointer, " ")) != NULL) && (numOfWords < maxWords)) {
           /* next call to strtok for the same string requires first parameter to be NULL */
            pointer = NULL;
            /* strncpy prevent copying more than maxLetters bytes */
            strncpy(allWords[numOfWords].word, token, maxLetters - 1);
            /* ensure null terminating byte */
            allWords[numOfWords].word[maxLetters - 1] = '\0'; 

            numOfWords++;
        }
    }
    /* don't forget to close the file */
    fclose(finput);
    return 0; // default ignore this
}

【讨论】:

  • @geforce 因为我忘记在内部while 循环中将pointer 设置为NULL,我已经修复了它。
  • 好吧,它现在不会崩溃并读取文本文件,但不是像以前那样读取文本文件中的所有单词
  • @geforce 你需要增加maxWords 很多,因为平均每行有 5 或 6 个单词,如果你说有超过 1000 行,那么我认为大约 8192 就可以了。
  • 我将两个 while 循环中的条件更改为“numOfWords
  • @geforce 当你增加maxWords时发生这种情况?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 2018-03-22
  • 1970-01-01
相关资源
最近更新 更多