C strcpy_s - 缓冲区太小&& 0错误

时间:2015-01-13 08:22:20

标签: c buffer

这行有缓冲问题     strcpy_s(*(pWords + word_count),word_length,pWord);     我试图从argv [1]中读取一个文件并打印出该文件中的每个单词以及它们的出现,但我无法弄清楚什么是错的......?!?

int main(int argc, char* argv[])
{
    char *delimiters = argv[2];                     // Prose delimiters
    char buf[BUF_LEN];                                       // Buffer for a line of keyboard input
    size_t str_size = INIT_STR_EXT;                          // Current memory to store prose
    char*  filePath = argv[1];
    FILE *fP ;
    char* pStr = malloc(str_size);                           // Pointer to prose to be tokenized
    *pStr = '\0';                                            // Set 1st character to null
    fopen_s(&fP, filePath, "r");
    fread(buf, BUF_LEN, 10, fP);





    size_t maxWords = 10;                                     // Current maximum word count
    int word_count = 0;                                       // Current word count
    size_t word_length = 0;                                   // Current word length
    char** pWords = calloc(maxWords, sizeof(char*));          // Stores pointers to the words
    int* pnWord = calloc(maxWords, sizeof(int));              // Stores count for each word

    size_t str_len = strnlen_s(buf, BUF_LEN);                // Length used by strtok_s()
    char* ptr = NULL;                                          // Pointer used by strtok_s()
    char* pWord = strtok_s(buf, delimiters, &ptr);  // Find 1st word

    if (!pWord)
    {
        printf("No words found. Ending program.\n");
        return 1;
    }

    bool new_word = true;                                     // False for an existing word
    while (pWord)
    {
        // Check for existing word
        for (int i = 0; i < word_count; ++i)
        if (strcmp(*(pWords + i), pWord) == 0)
        {
            ++*(pnWord + i);
            new_word = false;
            break;
        }

        if (new_word)                                            // Not NULL if new word
        {
            //Check for sufficient memory
            if (word_count == maxWords)
            { // Get more space for pointers to words
                maxWords += WORDS_INCR;
                pWords = realloc(pWords, maxWords*sizeof(char*));

                // Get more space for word counts
                pnWord = realloc(pnWord, maxWords*sizeof(int));
            }

            // Found a new word so get memory for it and copy it there
            word_length = ptr - pWord;      // Length of new word
            *(pWords + word_count) = malloc(word_length);         
            strcpy_s(*(pWords + word_count), word_length, pWord); // Copy to array
            *(pnWord + word_count++) = 1;                         // Increment word count
        }
        else
            new_word = true;                                      // Reset new word flag

        pWord = strtok_s(NULL, delimiters, &ptr);      // Find subsequent word
    }

3 个答案:

答案 0 :(得分:2)

strcpy_s在字符串的末尾添加一个空字节。您需要malloc(word_length+1)

答案 1 :(得分:1)

这一行存在两个问题:

fread(buf, BUF_LEN, 10, fP);

首先,当您阅读10个元素时,缓冲区太小了10倍。

其次,它不会比BUF_LEN(之前的* 10)更多地读取文件。

此外,代码不会处理newline个字符,因为我无法在argv[2]分隔符规范中传递该字符,即使是" \\n"

我建议您使用fread()循环替换fgets(),然后重新定义单词分隔符。

#define BUF_LEN 1000                        // plenty of room
...
char buf[BUF_LEN+1];                        // allow for 0 terminator
char delimiters[] = " \n\t";                // predefined
...
//size_t str_len = strnlen_s(buf, BUF_LEN); // unnecessary
while (fgets(buf, BUF_LEN, fP) != NULL) {   // new outer loop
    char* ptr = NULL;                       // carry on as you were
    ...
}

接下来,正如其他人所评论的那样,增加字符串空间分配

*(pWords + word_count) = malloc(word_length+1);

此外,虽然您使用了“安全”字符串函数,但未检查argcfopen_s()fread()malloc()中任何一项的结果, calloc()realloc(),您也没有关闭文件或释放内存。

答案 2 :(得分:0)

在我看来,你忘了为0字符添加一个额外的字节。 尽管如此:您可以使用SEEK_END获取fseek的文件大小,而使用偏移量0来分配大量内存+ 1字节,而不是为文件分配固定的缓冲区大小