从文件中读取特定行,并从C中的线程函数返回

发布于 2025-02-04 19:32:40 字数 2072 浏览 0 评论 0原文

我想从文件中读取特定行并从线程函数返回它,但是该函数不返回任何内容,

这是线程函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文