【问题标题】:C: Sorting order from file input, receiving an abundance of compilation errorC:从文件输入排序,收到大量编译错误
【发布时间】:2017-04-07 21:42:07
【问题描述】:

这是我第一次使用文件输入和指针,如果我的代码看起来一团糟,我很抱歉。我正在查看其他 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。

编辑:哎呀删除了图像。

【问题讨论】:

  • 对语句的malloc调用是否应该乘以1000而不是100?
  • 不要发布文字图片,而是发布文字。
  • 您的构建系统(或构建命令,您未与我们共享)放在一边的代码(其中的多个内容最终需要修复)已损坏。 Your code compiles(这绝不表示它是“正确的”)。
  • @JonnyHenly 哦,我现在看到了,你现在可以删除这条评论,我删除了我的。
  • 它也在这里编译和链接。

标签: c file sorting compilation stdin


【解决方案1】:

有几点意见:

  • 句子的最大大小小于单词的最大大小。最好使用常量或宏来避免这样的拼写错误。
  • 您正在使用argv[1](命令行参数),但不能保证用户将文件名作为参数发送。使用适当的消息更好地验证这一点(argc 是参数计数)。
  • fopen试图打开文件,如果不能打开,返回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)

【讨论】:

  • 您好,非常感谢您的意见!我结束了更多的研究,现在使用 fseek 而不是 fgets 并且我的代码按预期工作!感谢您指出常量+空闲内存分配,完全忽略了这一点!
  • 我想我的帖子回答了你原来的问题,你能接受吗?
  • 会的!抱歉第一次使用stackoverflow!
猜你喜欢
  • 1970-01-01
  • 2011-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 2013-02-13
相关资源
最近更新 更多