【问题标题】:Segmentation fault when using strlen使用 strlen 时出现分段错误
【发布时间】:2013-01-03 06:28:30
【问题描述】:

过去两天我一直在研究这个问题,但似乎无法找出问题所在。

我正在读取文件并将其作为字符串保存到 char 数组中,但是当我尝试在字符串上使用 strlen() 时,它会因分段错误而崩溃。

这就是我正在做的事情。

char read_string[25];
fscanf(file, "%s", &read_string);
int length = strlen(read_string);

另一个问题是当我将read_string 的内容复制到另一个字符数组时,它会在末尾添加随机字符。

这就是我正在做的事情

char input[25];
for(i=0; i<25; i++)
{
    if (read_string[i] == '.')
        break;
    else
        input[i] = read_string[i];
}

我需要在句点读取字符串到输入数组之前复制所有内容。

附:我正在使用fscanf(),因为该文件结构良好。

如果需要,这是我的代码。

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

main()
{
//variables
FILE *rFile; 
int num_states;                 //number of states
int num_alphabet;               //number of alphabets
int num_acstates;               //number of accepting states
int ac_state;                   //the accepting state
char alphabet[num_alphabet];    //alphabet
char read_string[25];   //assuming the input will be maximum of 25 character
char input[25];
int i,j,x;                      //indexes for the loop


//open the file to read
rFile = fopen("dfa11", "r");

if (rFile != NULL)
{
    //do the program here

    //read number of states and number of alphabets
    fscanf(rFile, "%d %d", &num_states, &num_alphabet);

    //read the alphabet
    fscanf(rFile, "%s", &alphabet); 

    //read the state transition table (matrix)
    int value = num_states*num_alphabet;
    char table[num_states][num_alphabet];

    for(i=0; i<num_states; i++)
    {
        for(j=0; j<num_alphabet; j++)
        {
            fscanf(rFile, "%d", &table[i][j]);
        }
    }

    //read number of accepting states and accepting state
    fscanf(rFile, "%d", &num_acstates);
    fscanf(rFile, "%d", &ac_state);

    //read the input string from the file
    fscanf(rFile, "%s", &read_string);

    for(i=0; i<25; i++)
    {
        if (read_string[i] == '.')
            break;
        else
            input[i] = read_string[i];
    }

    //strncpy(input, read_string, j);

    printf("%d\n", strlen(read_string));
    printf("%d\n", strlen(input));
    printf("%s\n", read_string);
    printf("%s\n", input);


    //close the file
    fclose(rFile);
}

else
{
    printf("File could not be found");
}
}

【问题讨论】:

  • gcc -Wall 告诉你warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[25]’。重要提示:始终使用 -Wall 进行编译并始终修复警告。

标签: c arrays segmentation-fault


【解决方案1】:

我正在从文件中读取并将其作为字符串保存到 char 数组中,但是当我尝试在字符串上使用 stelen 时,它会因分段错误而崩溃。

您不需要在fscanf 中取read_string 的地址,因为read_string 已经衰减为一个指针。无论如何,由于 &amp;read_string == read_string 在数组的情况下(类型除外),它不应该导致分段错误。

因此,问题来自其他地方。其中,检查文件中的行不超过 25 个字符。

另一个问题是当我将 read_string 的内容复制到另一个 char 数组中时,它会在末尾添加随机字符。

不要忘记在字符串末尾添加'\0'。

input[i] = '\0';

【讨论】:

  • 谢谢,我试过 &read_string 和 read_string。它们都对我的问题没有任何影响。另外,在字符串末尾添加 \0 是什么意思?字符串来自文件。
  • @user1993422:您的源代码中有很多错误。您可以尝试编写SSCCE 代码。例如,num_alphabet 未初始化并用作alphabet 数组的大小。此外,您肯定知道每个 string 都应该以 '\0' 结尾。在这里,input 没有这样的字符。
  • 但我仍然无法让 strlen(read_string) 工作。因为它仍然会导致段错误。
  • @user1993422:如我所说,您是否尝试编写 SSCEE 代码?当然这会解决问题。 ;-)
  • 让 strlen 也能正常工作 :) 尝试过 SSCEE 代码,猜猜它是我最好的新朋友。问题出在我的变量上,稍微改变了顺序就可以了。
【解决方案2】:

试试

fscanf(file, "%24s", read_string);

如果您的崩溃不再重现,这意味着您的文件包含超过 24 个字符的行。在这种情况下,您可以使缓冲区更大以避免此问题

编辑:

尝试在for循环后添加input[i] = '\0';

for(i=0; i<25; i++)
{
    if (read_string[i] == '.')
        break;
    else
        input[i] = read_string[i];
}
input[i] = '\0';

【讨论】:

  • 谢谢,但没有任何区别。
  • 如果您尝试过并且崩溃不再重现,这意味着崩溃的原因不是从文件读取时溢出
  • 是的,原因不是溢出。但我不能只是在我的程序中找到问题所在。我已经重新输入了 3 次程序。你能浏览一下我的程序并建议我应该做的任何改变吗?我会很感激它!
  • OMGG 非常感谢,空终止字符串成功了!不敢相信它从来没有发生在我身上。此外,我计划在修复此错误后立即使用这些变量。程序还没有完成。
猜你喜欢
  • 2016-03-25
  • 2015-08-14
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多