Home > Back-end >  For help, c simple problems
For help, c simple problems

Time:04-17

The code below
# include
# include
# include

Pthread_mutex_t counter_clock=PTHREAD_MUTEX_INITIALIZER;
Static int total_words=0;
Int main (int ac, char * av []) {
Void * count_words (void *);
If (ac! {
=3)Printf (" file1 file2 Useage: % s \ n ", av [0]).
exit(1);
}
Int error1 error2;
Pthread_t thread1 thread2;
Error1=pthread_create (& amp; Thread1, NULL, count_words, av [1]);
Error2=pthread_create (& amp; Thread2, NULL, count_words, av [2]);
Pthread_join (thread1, NULL);
Pthread_join (thread2, NULL);
Printf (" % d \ n ", total_words);
}
Void * count_words (void * f) {
Char * filename=f (char *);
The FILE * fp.
Int c, prevc='\ c;
If ((fp=fopen (filename, "r"))!=NULL) {
While ((c=getc (fp))!=(EOF)
{
if(! Isalnum (c) & amp; & Isalnum (prevc)) {
Pthread_mutex_lock (& amp; Counter_clock);
Total_words + +;
Pthread_mutex_unlock (& amp; Counter_clock);
}
Prevc=c;
}
fclose(fp);
}
The else {
Perror (filename);
}
return NULL;
}
This is a multi-threaded statistical word number of the program, the specific code is not important, I just want to ask the line
If ((fp=fopen (filename, "r"))!=NULL) {
When I change the double quotation marks to single quotes will appear Segmentation fault (core dumped) error, but I seem to have seen to use single quotes on the Internet, a great god answer, thank you

CodePudding user response:

Fopen (), for your reference: https://blog.csdn.net/weixin_33878457/article/details/85712854? Utm_medium=distribute. Pc_relevant_t0. None - task - 7 edefault % 7 eblogcommendfrommachinelearnpai2 blog - 2% % 7 edefault - 1. Control& Dist_request_id=1331978.858.16185879177551633 & amp; Depth_1 - utm_source=distribute. Pc_relevant_t0. None - task - 7 edefault % 7 eblogcommendfrommachinelearnpai2 blog - 2% % 7 edefault - 1. The control
Segmentation fault (core dumped) error, this sentence means Segmentation fault (core Dump), the core Dump is the operating system in the process of receiving certain signals and termination of the runtime, will at this time the content of the process's address space, and other information about the process status to write a disk file, the problem here, according to your description is the operation of the file to be parsed characters, so it gave the fopen () usage for reference links,
  • Related