Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序,用于使用线程计算文件中某个单词的出现次数获取分段错误_C_Segmentation Fault_Word Count - Fatal编程技术网

C程序,用于使用线程计算文件中某个单词的出现次数获取分段错误

C程序,用于使用线程计算文件中某个单词的出现次数获取分段错误,c,segmentation-fault,word-count,C,Segmentation Fault,Word Count,因此,我有以下问题:实现一个程序,该程序以文件名后跟单词作为参数。对于每个单词,创建一个单独的线程,计算其在给定文件中的外观。打印出所有单词的外观总和 我也不确定我的代码的格式是否正确。我试图找出如何在给定的文本文件中计算每个单词。我试图测试这段代码,但我从中得到了一些错误,最大的错误是分段错误。如果你有任何建议或帮助,请告诉我 我的代码是: #include <stdio.h> #include <stdlib.h> #include <strings.h>

因此,我有以下问题:实现一个程序,该程序以文件名后跟单词作为参数。对于每个单词,创建一个单独的线程,计算其在给定文件中的外观。打印出所有单词的外观总和

我也不确定我的代码的格式是否正确。我试图找出如何在给定的文本文件中计算每个单词。我试图测试这段代码,但我从中得到了一些错误,最大的错误是分段错误。如果你有任何建议或帮助,请告诉我

我的代码是:

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <pthread.h>

pthread_mutex_t mtx; // used by each of the three threads to prevent        other threads from accessing global_sum during their additions

int global_sum = 0;
typedef struct{
                char* word;
                char* filename;
}MyStruct;



void *count(void*str)
{
MyStruct *struc;
struc = (MyStruct*)str; 
const char *myfile = struc->filename;

FILE *f;
int count=0, j;
char buf[50], read[100];
// myfile[strlen(myfile)-1]='\0';
if(!(f=fopen(myfile,"rt"))){
     printf("Wrong file name");
}
else
     printf("File opened successfully\n");
     for(j=0; fgets(read, 10, f)!=NULL; j++){
         if (strcmp(read[j],struc->word)==0)
            count++;
     }

 printf("the no of words is: %d \n",count);  
 pthread_mutex_lock(&mtx); // lock the mutex, to prevent other threads    from accessing global_sum
 global_sum += count; // add thread's count result to global_sum
 pthread_mutex_unlock(&mtx); // unlock the mutex, to allow other  threads to access the variable
 }


int main(int argc, char* argv[]) {
int i;
MyStruct str; 

pthread_mutex_init(&mtx, NULL); // initialize mutex
pthread_t threads[argc-1]; // declare threads array 

for (i=0;i<argc-2;i++){

   str.filename = argv[1];  
   str.word = argv[i+2];

   pthread_create(&threads[i], NULL, count, &str); 
}

for (i = 0; i < argc-1; ++i)
     pthread_join(threads[i], NULL);

printf("The global sum is %d.\n", global_sum); // print global sum

pthread_mutex_destroy(&mtx); // destroy the mutex

return 0;

}
#包括
#包括
#包括
#包括
#包括
pthread\u mutex\u t mtx;//由三个线程中的每一个线程使用,以防止其他线程在其加法期间访问全局_和
int全局_和=0;
类型定义结构{
字符*字;
字符*文件名;
}我的结构;
void*计数(void*str)
{
MyStruct*struc;
struc=(MyStruct*)str;
const char*myfile=struc->filename;
文件*f;
int计数=0,j;
char buf[50],读取[100];
//myfile[strlen(myfile)-1]='\0';
如果(!(f=fopen(myfile,“rt”)){
printf(“错误的文件名”);
}
其他的
printf(“文件已成功打开\n”);
对于(j=0;fgets(read,10,f)!=NULL;j++){
如果(strcmp(读[j],struc->word)==0)
计数++;
}
printf(“字数为:%d\n”,计数);
pthread_mutex_lock(&mtx);//锁定互斥锁,以防止其他线程访问全局_sum
global\u sum+=count;//将线程的计数结果添加到global\u sum
pthread_mutex_unlock(&mtx);//解锁互斥锁,以允许其他线程访问变量
}
int main(int argc,char*argv[]){
int i;
MyStruct-str;
pthread_mutex_init(&mtx,NULL);//初始化互斥
pthread_t threads[argc-1];//声明线程数组

对于(i=0;i今天第二个好建议是仔细查看编译器给您的警告,并注意它们。
strcmp(阅读[j],struc->word)
你的编译器一定警告过你这是错误的。你正在传递一个
char
作为第一个参数,而不是
const char*
。这几乎肯定会导致seg错误。-kaylum

你得到的最好建议是使用调试器来帮助你发现问题。例如,在de中运行你的程序bugger将准确地告诉您是哪一行代码导致了seg故障。今天的第二个好建议是仔细查看编译器给您的警告,并注意它们。
strcmp(read[j],struc->word)
您的编译器一定警告过您这是错误的。您正在传递一个
字符作为第一个参数,而不是
常量字符*
。这几乎肯定会导致seg错误。