文件C编程中的最长和最短的字

你好,晚上好,

所以我正在用C编写一个程序,它将接受一个file.txt作为输入并读取文本。 程序应该读取文本文件,找到文件中最长和最短的单词,并在结束时打印出来。

我真的很接近,但是我遇到了一个段故障,不仅我不知道为什么,而且我不知道如何解决它。

这是代码:

#include  #include  FILE *fp; char str[60]; char *largest; char *smallest; char *word; int i, j; int main (int argc, char **argv) { // check that there are only two arguments if (argc == 2) { fp = fopen(argv[1], "r"); } // if not throw this error else { perror("Argument error."); return (-1); } // check if the file exists if (fp == NULL) { perror("Error opening file."); return (-1); } // set largest to first string and smallest to second largest = strcpy(largest, strtok(str, " ")); smallest = strcpy(smallest, strtok(NULL, " ")); word = strcpy(word, strtok(str, " ")); // while we get lines of the file while (fgets (str, 60, fp) != NULL) { // while the token string isn't empty while (word != NULL) { if (strlen(largest) > strlen(word)) { strcpy(word, largest); } if (strlen(smallest) < strlen(word)) { strcpy(word, smallest); } } } printf("The largest word in the file is: %s", largest); printf("The smallest word in the file is: %s", smallest); fclose(fp); return 0; } 

我很确定这是第二次循环…我还是不想使用它,但是我已经黑了很长时间了,这是我的逻辑所能想到的。

任何帮助,将不胜感激。 这是IS作业,虽然只是其中的一小部分,我并不是要求帮助解决整个问题。

此外,还有一个Makefile参与……我不认为这是重要的post,但随意问我,我会更新。

当我构建这个时,我可以确认该文件能够读取,我可以打印,放置和执行各种很酷的事情。 它只是在我尝试实现最长/最短单词的逻辑时才破坏。

谢谢!

您的逻辑存在一些问题。 请尝试以下代码

我做的几个假设是,

最大字长为20个字符。 您可以通过MAX_WORD_LENGTH宏更改它。 文件中的单词是空格分隔的最大行长度是60个字符

 #include  #include  #include  #define MAX_WORD_LENGTH 20 int main (int argc, char **argv) { FILE *fp; char str[60]; char *largest = (char*) malloc (MAX_WORD_LENGTH); char *smallest = (char*) malloc (MAX_WORD_LENGTH); int smallest_len = MAX_WORD_LENGTH, largest_len = 0; if (argc == 2) { fp = fopen(argv[1], "r"); } else { printf("Argument error."); return (-1); } if (fp == NULL) { printf("Error opening file."); return (-1); } while (fgets (str, 60, fp) != NULL) { char *temp = strtok(str, " "); while (temp != NULL) { if (strlen(temp) > largest_len) { strcpy(largest, temp); largest_len = strlen(largest); } if (strlen(temp) < smallest_len) { strcpy(smallest, temp); smallest_len = strlen(smallest); } temp = strtok(NULL, " "); } } printf("The largest word in the file is: %s\n", largest); printf("The smallest word in the file is: %s\n", smallest); fclose(fp); return 0; } 

argv [2]采用它满足您的需求

享受

 #include  #include  int main() { const int max_word_length = 60; char longest[max_word_length]; char shortest[max_word_length]; char current[max_word_length]; size_t longest_length = 0; size_t shortest_length = max_word_length; size_t current_length = 0; freopen("input", "r", stdin); freopen("output", "w", stdout); while (scanf("%s", current) > 0) { current_length = strlen(current); if ( current_length > longest_length) { longest_length = current_length; strcpy(longest, current); } if (current_length < shortest_length) { shortest_length = current_length; strcpy(shortest, current); } } printf("%s %s", shortest, longest); return 0; } 

所以我只是在线运行,看起来seg故障是由线路引起的

 largest = strcpy(largest, strtok(str, " ")); 

这是因为largest是调用中字符串副本的目标

 strcpy(largest, strtok(str, " ")); 

但它是一个指向什么都没有的指针。 它应该被声明为一个像这样的实际数组:

 char largest[60]; 

此外,当它尚未初始化为任何内容时,您不应该在str上调用strtok 。 它甚至不是一个合适的字符串,所以strtok在这一点上无法做任何有用的事情。