【问题标题】:Array of tokenized string literals in CC中的标记化字符串文字数组
【发布时间】:2018-03-23 15:36:03
【问题描述】:

我正在编写一个 C 程序来标记输入文本文件并跟踪单词长度的频率,同时跟踪和存储相应的单词本身。我的字数统计工作正常,但无法让我的 word_tracker 数组正确存储字符串:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX_LENGTH 34
#define MAX_WORDS 750

int main(int argc, char *argv[]){ 

    FILE *fp; //input file
    const char *cur; //stores current word as string literal
    char words[MAX_LENGTH*MAX_WORDS]; //stores all words from text file
    char file_name[100]; //stores file name
    int word_count[MAX_LENGTH] = {0}; //array to store frequency of words based on length
    const char *word_tracker[MAX_LENGTH][MAX_WORDS]; //array to store string literals of each word, indexed by char count and 
    int char_count; //current word's char count

    printf("Enter a file name: ");
    scanf("%s", file_name);
    fp = fopen(file_name, "r"); 

    if((fp==NULL)){
        printf("Failure: missing or unopenable file");
        return -1; 
    }else{
        while(fgets(words, sizeof(words), fp)){
            cur= strtok(words, " -.,\b\t\n"); //first word of line
            char_count = strlen(cur);
            word_count[char_count-1] = word_count[char_count-1]+1; //increment frequency of specific word length
            word_tracker[char_count-1][word_count[char_count-1]-1] = cur; //store string into corresponding array index location

            /*test printing*/
            printf("%d:", char_count-1); 
            printf("%s ", word_tracker[char_count-1][(word_count[char_count-1])-1]); 

            while(cur){
                    cur = strtok(NULL, " -.,\b\t\n"); //next word
                    if(cur){
                        char_count = strlen(cur);
                        word_count[char_count-1] = word_count[char_count-1]+1; //increment frequency of specific word length
                        word_tracker[char_count-1][word_count[char_count-1]-1] = cur; //store string into corresponding array index location

                        /*test printing*/
                        printf("%d:", char_count-1); //test print
                        printf("%s ", word_tracker[char_count-1][(word_count[char_count-1])-1]); //test print

                    }
                }
            }
        }
//Testing word_tracker: (This doesn't work)
    printf("\n\n%s \n", word_tracker[0][0]);
    printf("\n%s \n", word_tracker[1][0]);
    printf("%s \n", word_tracker[2][0]);
    printf("%s \n", word_tracker[3][0]);
    printf("%s \n", word_tracker[4][0]);
    printf("%s \n", word_tracker[5][0]);

    return 0;
}

“内部”测试(在标记循环内)运行良好,打印了正确的字符串和长度。但是,相对于输入文本文件所说的应该输入的内容,主打印末尾的打印测试看似随机字符串。关于我做错的事情,我有三个理论:

1) 我的索引错误

2) 我对如何填充和使用 char* 数组的理解不正确

3) 我的标记循环不正确(cur 不等于“孤立字符串”吗?)

我注意到在输入文件的最后一行中写入的任何内容的主要显示变体末尾的测试,所以我认为我的标记化循环可能是错误的。非常感谢任何指导,谢谢!

【问题讨论】:

  • 嗨,本,请不要误会,但你的代码是一团糟:P 你需要把事情分解成一些功能,这样你(和其他人)就可以遵循它很容易。
  • 你想存储一个二维字符数组,但是const char *word_tracker[MAX_LENGTH][MAX_WORDS];是一个指向二维字符数组的指针,去掉声明中的*,注意你需要const char word_tracker[MAX_WORDS][MAX_LENGTH]; 而不是const char word_tracker[MAX_LENGTH][MAX_WORDS];,最后,const 使你的数组​​不可写(只读),删除const 部分:)
  • @KeineLust 在我看来,OP 正在尝试创建一个 strings 的二维数组,其中 word[i][j] 是长度为 i 的字符串编号 j .
  • 我认为将代码拆分成函数是个好主意。试一试,它甚至可以帮助您解决问题:P 甚至可以为它提出更好的设计。如果你在一次很好的尝试后还在苦苦挣扎,我会给你一个合理的方法来解决这个问题:P
  • 考虑使用strcspn(返回初始子字符串的长度)而不是strtok。

标签: c arrays string pointers tokenize


【解决方案1】:

您的结果数组当前是const char *word_tracker[MAX_LENGTH][MAX_WORDS],它是指针 的二维数组。 您可以 (a) 使用 1D 指针数组并为找到的每个单词分配内存,或者 (b) 使用 2D 字符数组和 strcpy 每个单词在适当的位置。

所以(a)看起来像......

const char *word_tracker[MAX_WORDS];
...
word_tracker[someIndexWithSomeMeaningUpToMAX_WORDS] = strdup(cur);

而 (b) 看起来像

char word_tracker[MAX_WORDS][MAX_LENGTH];
...
strncpy(word_tracker[someIndexWithSomeMeaningUpToMAX_WORDS], cur, MAX_LENGTH);
word_tracker[someIndexWithSomeMeaningUpToMAX_WORDS][MAX_LENGTH-1] = '\0'

请注意,在 (b) 中,MAX_LENGTH 表示字符串(即单个单词)的最大长度,因此是第二个索引。 strncpy 确保不超过为单词保留的大小。

【讨论】:

  • const char word_tracker[MAX_WORDS][MAX_LENGTH]; strncpy(word_tracker[someIndexWithSomeMeaningUpToMAX_WORDS], cur, MAX_LENGTH); ---> "传递 'strncpy' 的参数 1 从指针目标类型 [-Wdiscarded-qualifiers] 中丢弃 'const' 限定符"
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
相关资源
最近更新 更多