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() ?