Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3396130
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:22:15+00:00 2026-05-18T04:22:15+00:00

Im making an application that uses of API-threads in C, The program takes N-files

  • 0

Im making an application that uses of API-threads in C, The program takes N-files (N>2) with names disordered,per each file is generated a thread of execution which sort the files using the function qsort, after being ordered files, each thread should create a file keeping the original file intact and displaying the sorted file to another file with the extension <.sorted>. The program sorts the numbers without problems, even if I put standard output displays the result on screen, but when I try to create the output file with extension .sorted the program breaks out.

this is my code file.c

#include <stdio.h>              /* Standard buffered input/output        */
#include <stdlib.h>             /* Standard library functions            */
#include <string.h>             /* String operations                     */
#include <pthread.h>        /* Thread related functions              */
#include "pf1.h"        /* Header specific to this app           */



pthread_attr_t attr;

void *thread_worker(void *name_file)
{

FILE *entrada, *salida;
char* nombres = (char*)name_file; 
int numero;
char temp [10000];

int i;
stats_t estadisticas;

printf ("File_name:%s\n", nombres);
entrada = fopen(nombres, "r");
salida =  fopen (strcat(nombres, ".sorted"), "w");

while (!feof(entrada)){
fscanf (entrada, "%s\n",temp);
numero++;
}

char* lista[numero]; //array to sort the file
rewind (entrada);

    for (i=0;i<numero;i++) 
    {

        fscanf(entrada," %[^\n]", temp);


        lista[i] = (char*)malloc((strlen(temp)+1)*sizeof(char));

        strcpy(lista[i], temp);

    }


size_t large = sizeof(lista) / sizeof(char *);



qsort(lista,large ,sizeof(char *) ,cstring_cmp );

printf ("Archivo Ordenado\n", i+1);

for (i=0;i<large;i++)


printf("%s\n",lista[i]);


 pthread_exit(NULL);
}


int main(int argc, char *argv [])
{

stats_t **stats;
int i, rc;
 pthread_t my_threads[argc-1]; 
 pthread_attr_init(&attr);

if (argc <3) {
printf ("|files| > 2\n");
}else{
printf("files to sorted: %d\n", argc - 1);

for (i = 1; i < argc; i++){ 

    //printf("%s%s\n", argv[i], (i < argc-1) ? " " : "");

rc = pthread_create(&my_threads[i], &attr, thread_worker, (void *)argv[i]);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n",rc);
         return -1;
      }
}

}
  return 0;

} /*end main */

this is mi file.h

#ifndef PF1_H_

#define PF1_H_





typedef struct _stats_t

{

    char *longest, *shortest;

    unsigned int numlines;

} stats_t;

int cstring_cmp(const void *a, const void *b)

{

    const char **ia = (const char **)a;

    const char **ib = (const char **)b;

    return -strcasecmp(*ia, *ib);

    /* strcmp functions works exactly as expected from

    comparison function */

}


void print_cstring_array(char **array, size_t len)

{

    size_t i;



    for(i=0; i<len; i++)

        printf("%s | ", array[i]);



    putchar('\n');

}




#endif /* PF1_1_H_ */

I would like some help with this problem because I can not see which is the fault … thanks to all in advance and excuse my English

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:22:16+00:00Added an answer on May 18, 2026 at 4:22 am

    This line here may be your problem:

    salida =  fopen (strcat(nombres, ".sorted"), "w");
    

    From what I can tell, that nombres variable is coming from argv. Since you’re not the one allocating memory for argv, you don’t know that there will be extra space for the “.sorted” (and there probably won’t be). If you strcpy it to your own buffer with space for the “.sorted”, you should be fine.

    #define EXT_LEN 7
    #define MAX_TOTAL_LEN 250
    #define MAX_FILE_LEN 242 //MAX_TOTAL_LEN - EXT_LEN - 1
    
    char *name_ptr;
    char nombres[MAX_TOTAL_LEN];
    int len;
    
    name_ptr = (char*)name_file;
    len = strlen(name_ptr);
    if (len > MAX_FILE_LEN) {
        len = MAX_FILE_LEN;
    }
    strncpy(nombres, name_ptr, len);
    strcpy(nombres+len, ".sorted");
    salida =  fopen (nombres, "w"); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is beyond both making sense and my control. That being said here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.