C:从文件输入排序,收到大量编译错误

C: Sorting order from file input, receiving an abundance of compilation error

这是我第一次使用文件输入和指针,如果我的代码看起来一团糟,我深表歉意。我正在查看其他 Stack Overflow 解决方案作为参考。我的代码旨在查找文件中最长的单词,我可以假设输入文本中的单词不会超过 1000 个字符。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// main() must return an integer
int main(int argc, char** argv) {
    FILE *file; //open an existing file
    file = fopen(argv[1], "r"); //using argv as a pointer to an array of pointers to arrays of characters
    char *sentence = (char *) malloc(100 * sizeof(char)); //memory allocation, unsure what to use for size*


    //fgets - reads a line from the specified stream and stores it into the string pointed to
    //max amount set to 1000
    while (fgets(sentence, 1000, file) != NULL) {
        char *word;
        int maxlen = 0;
        char *maxW;
        //max size of 1000 characters
        maxW = (char *) calloc(1000, sizeof(char));
        word = (char *) calloc(1000, sizeof(char));
        word = strtok(sentence, " ");  //using strtok to break sentance to token

        //checking size of word with maxlen
        while (word != NULL) {
            if (strlen(word) > maxlen) {
                maxlen = strlen(word);
                strcpy(maxW, word);
            }
            word = strtok(NULL, " ");
        }
        printf("%s\n", maxW); //printing the max sized word
        maxlen = 0; //reset

        return 0;
    }
}

我只在 windows 中使用命令行来使用 gcc 编译我的代码,我尝试使用 CLion,但我现在不知道如何使用 CLion。

编辑:哎呀删除了图像。

有几点观察:

  • 句子的最大长度小于单词的最大长度。最好使用常量或宏来避免这样的拼写错误。
  • 您正在使用 argv[1](命令行参数),但无法保证用户会发送文件名作为参数。最好用适当的消息来验证这一点(argc 是参数计数)。
  • fopen 尝试打开文件,如果无法打开,returns NULL,您的代码必须进行验证。
  • 代码为每一行分配了句子、单词和 maxW,但这是不正确的,所以将这些分配移到主代码的顶部。
  • word 只是指向 sentence 的指针,所以它根本不需要分配内存。
  • 你的程序只读到第一行就停止了,那是因为它在while里面有一个return 0;
  • 始终free 您使用malloc/calloc分配的内存。

除了这些观察之外,您的代码就差不多了。

重新组织您的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Using constants avoid typos
#define MAX_SIZE    1000

int main(int argc, char** argv) {
    // Tokens not considered as part of a word
    const char tokens[] = " \t\n\r,/:[]=().<>";
    int maxlen = 0;
    // calloc(a,b) == malloc(a*b)
    char *sentence = (char *) malloc(MAX_SIZE * sizeof(char)); 
    char *maxW     = (char *) calloc(MAX_SIZE, sizeof(char));
    char *word;
    // try open and validate
    FILE *file     = fopen(argv[1], "rt"); 
    if(file == NULL){
        printf("file '%s' cannot be opened!\n", argv[1]);
        return 1; // !=0 means error to OS
    }

    while (fgets(sentence, MAX_SIZE, file) != NULL) {
        word = strtok(sentence, tokens);  
        while (word != NULL) {
            if (strlen(word) > maxlen) {
                maxlen = strlen(word);
                strcpy(maxW, word);
            }
            word = strtok(NULL, tokens);
        }
    }
    printf("Max word='%s' (len=%d)\n", maxW, maxlen); 

    // Don't forget free memory allocated with malloc/calloc
    free(sentence);
    free(maxW);

    return 0;
 }

并执行同一个程序文件:

$ gcc program.c
$ ./program program.c
Max word='considered' (len=10)