从文件中读取特定行,并从C中的线程函数返回
我想从文件中读取特定行并从线程函数返回它,但是该函数不返回任何内容,
这是线程函数:
static void* read_line(void* th_arg){
struct arg* current = (struct arg*)th_arg;
char buffer[MAX_LINE_SIZE];
char* return_str;
bool keep_reading = true;
int current_line = 1;
do{
fgets(buffer, sizeof(buffer), current->file_name);
if(feof(current->file_name)){
keep_reading = false;
}
else if(current_line++ == current->line){
keep_reading = false;
return_str = buffer;
pthread_exit((void*)return_str); // return the string
}
} while(keep_reading);
printf("\n no return from the thread function\n");
pthread_exit(NULL); // return NULL
}
创建线程的函数:
bool check_account(struct info* temp, char* email, char* password){
char fileName[FILE_NAME_SIZE];
strcpy(fileName,temp->folder_name);
strcat(fileName,"/");
strcat(fileName,email);
strcat(fileName,".txt");
if(check(fileName)){
FILE* check = fopen(fileName,"r");
pthread_t email, password;
// if check == NULL means the file has not been open
if(check == NULL) return false;
struct arg* temp1 = (struct arg* )malloc(sizeof(struct arg));
struct arg* temp2 = (struct arg* )malloc(sizeof(struct arg));
char treturn1[10];
char* treturn2;
// set the data of the 2 member of struct created
set_struct_data(temp1,check,2);
set_struct_data(temp2,check,4);
printf("set succes\n");
// create 2 thread functions
pthread_create(&email, NULL, &read_line,(void*)temp1);
pthread_create(&password, NULL, &read_line,(void*)temp2);
// wait the thread to return the values
pthread_join(email,(void**)&treturn1);
pthread_join(password,(void**)&treturn2);
free(temp1);
printf("free 1 succces\n");
free(temp2);
printf("free 2 succes\n");
fclose(check);
return true;
}
return false;
}
这是线程函数的参数:
struct arg{
FILE *file_name;
int line;
struct arg* next;
};
是问题从阅读还是从pthread_join()?
I want to read a specific line from file and return it from thread function but the function don't return any thing
this is the thread function:
static void* read_line(void* th_arg){
struct arg* current = (struct arg*)th_arg;
char buffer[MAX_LINE_SIZE];
char* return_str;
bool keep_reading = true;
int current_line = 1;
do{
fgets(buffer, sizeof(buffer), current->file_name);
if(feof(current->file_name)){
keep_reading = false;
}
else if(current_line++ == current->line){
keep_reading = false;
return_str = buffer;
pthread_exit((void*)return_str); // return the string
}
} while(keep_reading);
printf("\n no return from the thread function\n");
pthread_exit(NULL); // return NULL
}
the function that create the thread:
bool check_account(struct info* temp, char* email, char* password){
char fileName[FILE_NAME_SIZE];
strcpy(fileName,temp->folder_name);
strcat(fileName,"/");
strcat(fileName,email);
strcat(fileName,".txt");
if(check(fileName)){
FILE* check = fopen(fileName,"r");
pthread_t email, password;
// if check == NULL means the file has not been open
if(check == NULL) return false;
struct arg* temp1 = (struct arg* )malloc(sizeof(struct arg));
struct arg* temp2 = (struct arg* )malloc(sizeof(struct arg));
char treturn1[10];
char* treturn2;
// set the data of the 2 member of struct created
set_struct_data(temp1,check,2);
set_struct_data(temp2,check,4);
printf("set succes\n");
// create 2 thread functions
pthread_create(&email, NULL, &read_line,(void*)temp1);
pthread_create(&password, NULL, &read_line,(void*)temp2);
// wait the thread to return the values
pthread_join(email,(void**)&treturn1);
pthread_join(password,(void**)&treturn2);
free(temp1);
printf("free 1 succces\n");
free(temp2);
printf("free 2 succes\n");
fclose(check);
return true;
}
return false;
}
and this is the argument of the thread function:
struct arg{
FILE *file_name;
int line;
struct arg* next;
};
is the problem from reading or from pthread_join() ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论