linux下C语言中的分段错误

linux下C语言中的分段错误,c,linux,segmentation-fault,C,Linux,Segmentation Fault,我试图在linux ubuntu中运行此C编程。我遇到一个错误分段错误(内核转储)。我试图读取一个文本文件,其中包含以下格式的行: key01 value01 key02 value02 key03 value03 这是我的节目 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_ARY 50 int main() { int i=0; int n

我试图在linux ubuntu中运行此C编程。我遇到一个错误分段错误(内核转储)。我试图读取一个文本文件,其中包含以下格式的行:

key01 value01
key02 value02
key03 value03 
这是我的节目

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

#define MAX_ARY 50
int main() 
{
    int i=0;
    int numProgs=0;
    char* lines[MAX_ARY];
    char line[40];
    int j;
    char search_key[1000];
    int position=6, length=5, c = 0;
    char str[10];
    int ret=0;
    char* token;
    char* my_key;    

    FILE *file;
    file = fopen("client1.txt", "r");

    while(fgets(line, sizeof line, file)!=NULL) 
        {
            //check to be sure reading correctly
            //printf("%s", line);
            //add each filename into array of programs
            lines[i]=malloc(sizeof(line));
            strcpy(lines[i],line);
            i++;
            //count number of programs in file
            numProgs++;
        }

    //check to be sure going into array correctly 
    for (j=0 ;j<numProgs+1;j++) 
        {
            //printf("%s", lines[j])
            ;
        }

    printf("Please enter your search:");
    scanf("%s",str);


    for(i = 0; i<numProgs; i++)
        {    
            // Gettng the univ names from the list
            token = strtok(lines[i]," ");

            /* walk through other tokens */
            while( token != NULL )
                {
                    ret=strcmp(str, token);
                    printf("str=%s\t token=%s ret=%d\n", str, token, ret);
                    token = strtok(NULL," ");

                    if(ret==0)
                        {
                            //now token should be my required token, so break here
                            break;
                        }
                }
            if (ret == 0)
                break;
        }

    printf("Required substring is: %s\n", token);
    //send(token,to-server1); 
    fclose(file);
    return 0;

}
#包括
#包括
#包括
#定义最大值50
int main()
{
int i=0;
int numProgs=0;
字符*行[最大值];
字符行[40];
int j;
字符搜索_键[1000];
内部位置=6,长度=5,c=0;
char-str[10];
int-ret=0;
字符*令牌;
我的钥匙;
文件*文件;
file=fopen(“client1.txt”,“r”);
while(fgets(行、行大小、文件)!=NULL)
{
//检查以确保读数正确
//printf(“%s”,第行);
//将每个文件名添加到程序数组中
行[i]=malloc(sizeof(行));
strcpy(行[i],行);
i++;
//计算文件中的程序数
numProgs++;
}
//检查以确保正确进入阵列

对于(j=0;j我刚刚尝试了您的代码的这个版本(实际上它与您的代码相同,只是有两个小改动)

#包括
#包括
#包括
#定义最大值50
int main()
{
int i=0;
int numProgs=0;
字符*行[最大值];
字符行[40];
int j;
字符搜索_键[1000];
内部位置=6,长度=5,c=0;
char-str[10];
int-ret=0;
字符*令牌;
我的钥匙;
文件*文件;
file=fopen(“client1.txt”,“r”);
while(fgets(行、行大小、文件)!=NULL)
{
//检查以确保读数正确
//printf(“%s”,第行);
//将每个文件名添加到程序数组中
行[i]=malloc(sizeof(行));
strcpy(行[i],行);
i++;
//计算文件中的程序数
numProgs++;
}
//检查以确保正确进入阵列

对于(j=0;jj)您的代码可能有很多可能解释分段错误的原因,基本上在许多地方都有可能发生缓冲区溢出,最值得注意的是
scanf(“%s”,str);
因为
sizeof(str)==10
,而且
malloc(sizeof(line))
可能是
malloc(strlen(line)+1)
相反,如果您只想复制读取的字节,请更改为
scanf(“%9”,str);
并检查问题是否仍然存在。如果您还没有学会使用gdb,这将是一个很好的开始时间。如果您在启用调试符号的情况下编译,gdb可以准确地告诉您是否发生了SEGFULT,并让您检查相关的变量值以查看出了什么问题。请检查
fopen()的返回代码。
(在您的情况下,它是
文件
变量)。
fopen()
在出现错误时可以返回
NULL
。尝试进一步使用
文件
变量(实际上是指针)可能会导致seg故障(在取消引用此指针时)。请记住,在继续之前,请始终检查库函数/系统调用的返回码。显示输入和输出。文本文件中有多少行?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_ARY 50
int main()
{
    int i=0;
    int numProgs=0;
    char* lines[MAX_ARY];
    char line[40];
    int j;
    char search_key[1000];
    int position=6, length=5, c = 0;
    char str[10];
    int ret=0;
    char* token;
    char* my_key;

    FILE *file;
    file = fopen("client1.txt", "r");

    while(fgets(line, sizeof line, file)!=NULL)
    {
        //check to be sure reading correctly
        //printf("%s", line);
        //add each filename into array of programs
        lines[i]=malloc(sizeof(line));
        strcpy(lines[i],line);
        i++;
        //count number of programs in file
        numProgs++;
    }

    //check to be sure going into array correctly
    for (j=0 ;j<numProgs;j++)
    {
        printf("%s", lines[j]);
    }

    printf("Please enter your search:");
    scanf("%s",str);


    for(i = 0; i<numProgs; i++)
    {
        // Gettng the univ names from the list
        token = strtok(lines[i]," ");

        /* walk through other tokens */
        while( token != NULL )
        {
            ret=strcmp(str, token);
            printf("str=%s\t token=%s ret=%d\n", str, token, ret);
            token = strtok(NULL," ");

            if(ret==0)
            {
                //now token should be my required token, so break here
                break;
            }
        }
        if (ret == 0)
            break;
    }

    printf("Required substring is: %s\n", str);
    //send(token,to-server1);
    fclose(file);
    return 0;

}