C 分段断层与构造

C 分段断层与构造,c,segmentation-fault,C,Segmentation Fault,我希望这个程序读取一个输入文件,然后将单个单词及其频率保存到一个结构中,然后将该结构的内容打印到一个输出文件中。我有分段错误,但我不知道为什么。我也不确定我是否正确地编写了程序。谢谢你的帮助 #include "stdio.h" #include "stdlib.h" #include "string.h" struct string_count { char *str; int *str_freq; }; int main(int argc, char* argv[]) { int i=0

我希望这个程序读取一个输入文件,然后将单个单词及其频率保存到一个结构中,然后将该结构的内容打印到一个输出文件中。我有分段错误,但我不知道为什么。我也不确定我是否正确地编写了程序。谢谢你的帮助

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

struct string_count
{
char *str;
int *str_freq;
};

int main(int argc, char* argv[])
{
int i=0, j, temp;
char *str_temp, *temp2;
struct string_count *word ;
FILE *fp1, *fp2;

fp1 = fopen("input.txt", "r");
if( fp1 == NULL )
{
     printf("Input file can't be opened");
     exit(1);
}    
fp2 = fopen("output.txt", "w");
if (fp2 == NULL)
{ 
    printf("Error: Output file can't be created");
    exit (1);
}
fgets(str_temp, 50000000, fp1);
str_temp = (char*)malloc(5000000*sizeof(char));
fclose(fp1);
word->str = (char*)malloc(strlen(str_temp)*sizeof(char));
word->str_freq = (int*)malloc(strlen(str_temp)*sizeof(int));
word->str[0] = (char*)strtok(str_temp, " ,;(){}""'");
word->str_freq[0] = 0;
word->str_freq[0]++;
while(getc(fp2) != EOF)
{
    i++;
    word->str[i] = (char*)strtok(NULL, " ,;(){}""'");
    for (j=0, word->str[i]=0;j < i;j++)
    {
        if( word->str[i] = word->str[j]) word->str_freq[i]++;
    }
}
for (j = 1; j < i; j++)
{
    if (word->str_freq[j] > word->str_freq[j - 1])
    {
          temp = word->str_freq[j];
          word->str_freq[j] = word->str_freq[j - 1];
          word->str_freq[j - 1] = temp;
          str_temp = word->str[j];
          word->str[j] = word->str[j - 1];
          word->str[j - 1] = str_temp;
    }   
}
for (j = 0; j < i; j++)
{
    fprintf(fp2, "%s    %d\n", word->str[j], word->str_freq[j]);             
}
fclose(fp2);
system("PAUSE");
}
#包括“stdio.h”
#包括“stdlib.h”
#包括“string.h”
结构字符串\u计数
{
char*str;
int*str_freq;
};
int main(int argc,char*argv[])
{
int i=0,j,温度;
字符*str_temp,*temp2;
结构字符串_count*字;
文件*fp1,*fp2;
fp1=fopen(“input.txt”,“r”);
如果(fp1==NULL)
{
printf(“无法打开输入文件”);
出口(1);
}    
fp2=fopen(“output.txt”,“w”);
如果(fp2==NULL)
{ 
printf(“错误:无法创建输出文件”);
出口(1);
}
fgets(str_temp,50000000,fp1);
str_temp=(char*)malloc(5000000*sizeof(char));
fclose(fp1);
word->str=(char*)malloc(strlen(stru temp)*sizeof(char));
word->str_freq=(int*)malloc(strlen(stru temp)*sizeof(int));
word->str[0]=(char*)strtok(str_temp,;(){}'”;
word->str_freq[0]=0;
word->str_freq[0]+;
而(getc(fp2)!=EOF)
{
i++;
word->str[i]=(char*)strtok(NULL,,;(){}'');
对于(j=0,word->str[i]=0;jstr[i]=word->str[j])word->str_freq[i]++;
}
}
对于(j=1;jstr_-freq[j]>word->str_-freq[j-1])
{
temp=word->str_freq[j];
word->str_freq[j]=word->str_freq[j-1];
word->str_freq[j-1]=temp;
str_temp=word->str[j];
word->str[j]=word->str[j-1];
word->str[j-1]=str_temp;
}   
}
对于(j=0;jstr[j],word->str_freq[j]);
}
fclose(fp2);
系统(“暂停”);
}

你首先犯了这么多错误

str_temp=(char*)malloc(5000000*sizeof(char))

在fgets行之前和此处也使用它

word->str=(char*)malloc(strlen(stru temp)*sizeof(char))


在这里,您没有将内存分配给word结构指针,也没有进一步将内存分配给其其他成员。请纠正这些问题

您想在这里做些什么?word->str[0]=(char*)strtok(str_temp,;(){}'”;尝试使用gdbn进行调试不需要调试器:
word->str=
word此时未初始化。str\u temp还有一个问题。您先执行FGET,然后执行malloc,先执行malloc,然后执行FGETSY您在分配之前使用str_temp。切换fgets和malloc语句。此外,stdio.h和任何系统标头都应该在中。使用==进行比较:您正在使用=。你那一类也不行。