c - 在C中的字结构数组中存储元素

标签 c file structure text-files

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

文本文件(输入):

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
}

最佳答案

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

(numOfWords < maxWords)

以及我在源代码中添加注释的其他一些问题,我还使用 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
}

关于c - 在C中的字结构数组中存储元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27892699/

相关文章:

ios - 从 PHImageFileURLKey 获取的 URL 读取

java - 删除目录Java中的文件类型

c - 在C中有效地初始化结构内部的指针

java - 组织基于 Java 的游戏的源代码

Java:如何创建一个接受第一个元素的列表的列表 cons

C-将矩阵传递给没有非常量列值的函数

c++ - C 与 C++ 中的 Pthread 之间的差异

将指针转换为 float 或指向带有指针参数的函数

c - 吉尼 错误代码 139 : Segmentation fault (Core dumped) using C

java - 在 Java SE 6 中 move 文件并进行错误处理