2015-05-18 39 views
0

我有一个程序必须从文件中获得最长的句子 为了实现这个目的,我将第一句放在一个数组中,然后将未来的句子与当前最大的句子进行比较句子的大小代码不断崩溃(4.2周已停止工作

然而,比较这两个数组的行为并没有使我躲避 数组的current_sentence和longest_sentence都是80个字符长,但我想知道哪个实际上包含最长的句子(最长可以有80个字符)

我已经尝试了很多不同的解决方案(通过谷歌,其中大部分是stackoverflow的结果),但每次我尝试时返回的值是文件中的第一句,这使我相信检查本身完全失败,或者两个数组的长度都返回为80.

这些尝试包括(但是不限于):

if((sizeof(current_sentence)/sizeof(char)) < (sizeof(longest_sentence)/sizeof(char)) 

if(sizeof(current_sentence)/sizeof(current_sentence[0])) < (sizeof(longest_sentence)/sizeof(longest_sentence[0])) 

这里是我的代码:

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

char *langste_regel(char *); 

int main(void) { 
    /* 
    * stdout and stderr required for proper output 
    */ 
    setvbuf(stdout, NULL, _IONBF, 0); 
    setvbuf(stderr, NULL, _IONBF, 0); 

    char *s = langste_regel("vb1.txt"); 
    if(s != NULL) { 
     printf("\nde langste regel is: %s\n", s); 
     free(s); 
    } 

    return 0; 
} 

char *langste_regel(char *filename) { 
    FILE *file; 
    file = fopen(filename, "r"); 

    if(file == NULL) { 
     fprintf(stderr, "Kan bestand niet %s openen", filename); 
    } 

    char current_sentence[80]; 
    int len = 2; 
    char *longest_sentence = (char *)malloc(sizeof(char) * len); 

    fgets(longest_sentence, 80, file); 

    while(fgets(current_sentence, 80, file)) { 
     if(sizeof(current_sentence) < sizeof(longest_sentence)) { 
      strncpy(longest_sentence, current_sentence, 80); 
     } 
    } 

    fclose(file); 

    return longest_sentence; 
} 
+0

使用'strlen()'而不是'sizeof'。 'sizeof'是一个运算符,用于计算变量的大小。 'strlen()'是一个返回字符串长度的函数。 –

回答

3

你想用strlen()得到线(S)的长度,不sizeof返回的对象占用的字节数。

变化

if(sizeof(current_sentence) < sizeof(longest_sentence)) { 

if(strlen(current_sentence) < strlen(longest_sentence)) { 

我看到的另一个问题是,你只分配2个字节,但读取高达80个字节的位置:

char *longest_sentence = (char *)malloc(sizeof(char) * len); 

    fgets(longest_sentence, 80, file); 
+0

是的,我知道我需要分配数组,所以我可以返回它的值(否则我会得到一个局部变量错误)。我不完全知道len变量在这个情景中是如何工作的。我可以将2更改为80,但它不再是动态的(?)。或者我将它设置为80,然后从那里自动增加? –

+0

PS:我刚测试过它,它的工作原理,包括80字节的分配。原来我的比较也是错误的,我需要'strlen(current_sentence)> strlen(longest_sentence)'而不是'strlen(current_sentence)

+0

*我可以将2更改为80,但它不再是动态的(?)*为什么不呢?如果你不知道行的实际长度,并且不确定它们是否不会超过,那么你需要通过字符读取char并使用'realloc'来增加内存。 –

1

代码

sizeof(current_sentence) 

是不是在做你的想法。要查找以空字符结尾的字符串的长度,请使用

strlen(current_sentence) 
1

您的代码有许多问题。

file = fopen(filename, "r"); 

    if(file == NULL) { 
     fprintf(stderr, "Kan bestand niet %s openen", filename); 
    } 

您检查fopen()呼叫不成功,但你只是继续前进并使用这file指针。你应该向调用者返回一个错误指示。


char current_sentence[80]; 
    int len = 2; 
    char *longest_sentence = (char *)malloc(sizeof(char) * len); 

    fgets(longest_sentence, 80, file); 

您分配2个字节来longest_sentence,然后您尝试读取多达80字节到缓冲区。你应该分配80个字节。

如果您打算根据需要动态增加缓冲区,则需要更复杂的解决方案。您需要:

  • 尝试分配80个字节。
  • 尝试读取80个字节。
  • 检查字符串是否以换行符结尾(\n)。
  • 如果不是,请尝试将realloc()缓冲区设置为较大的大小。
  • 继续读取和重新分配,直到

    • 一个换行符已经发现,或
    • 你已经达到了最终的文件,或
    • 发生读取错误,或
    • 重新分配失败或
    • 您达到预定义的最大长度。

您还没有检查字符串被成功读取。 fgets()函数将在文件结束或发生读取错误时返回NULL。你应该向调用者返回一个错误指示符。例如:

if (!fgets(longest_sentence, 80, file)) { 
    free (longest_sentence); 
    return NULL: 
} 

while(fgets(current_sentence, 80, file)) { 
     if(sizeof(current_sentence) < sizeof(longest_sentence)) { 
      strncpy(longest_sentence, current_sentence, 80); 
     } 
    } 

sizeof符的结果是操作数,而不是串的长度的类型的大小。你应该使用strlen()(并且反转比较,正如你在别处注意到的那样)。

while(fgets(current_sentence, 80, file)) { 
    if(strlen(current_sentence) > strlen(longest_sentence)) { 
     strncpy(longest_sentence, current_sentence, 80); 
    } 
} 

使用strncpy()通常是有问题的。上面的调用将总是写80字节,不管current_sentence的长度。一般来说,如果在输入字符串的前80个字节内没有找到零,它将而不是零终止输出字符串。但是,这将在这个的情况下,因为fgets()保证这80个字符中有一个零字节。

简单的strcpy()在这里会更直接(在我看来)。