I am currently working on an assignment where I have to extract text one char at a time from each file and then concatenate them and print it out. I have found a way to extract my characters and save them in variable a-d. How do I add these char to my char array to print out to the end? I initially malloc it to the size of a char and at each if statement I realloc. All the guides I found online recommend I use strcat but I cannot pass in the a-d variables to append. How would I fix this?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
pthread_mutex_t thread1;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
char *output;
void* processing(void* file) {
FILE *test = fopen("drive1.data", "r");
FILE *drive1 = fopen("drive1.data", "r");
FILE *drive2 = fopen("drive2.data", "r");
FILE *drive3 = fopen("drive3.data", "r");
FILE *drive4 = fopen("drive4.data", "r");
FILE *drive5 = fopen("drive5.data", "r");
int c, a, b, e, d;
int control = 0;
//Initializingn the char array with initial size 1
output = (char *)malloc(sizeof(char));
while(!feof(drive1)) {
if(control == 0) {
pthread_mutex_lock(&thread1);
//printf("Mutex lock\n");
c = getc(drive1);
output = realloc(output, sizeof(output) + sizeof(char));
printf("%c\n", (char)c);
strcat(output, (char)c);
control++;
pthread_mutex_unlock(&thread1);
//printf("Mutex unlock\n");
} else if(control == 1) {
pthread_mutex_lock(&thread1);
//printf("Mutex lock\n");
a = getc(drive2);
printf("%c\n", (char)a);
control++;
pthread_mutex_unlock(&thread1);
//printf("Mutex unlock\n");
} else if(control == 2) {
pthread_mutex_lock(&thread1);
//printf("Mutex lock\n");
b = getc(drive3);
printf("%c\n", (char)b);
control++;
pthread_mutex_unlock(&thread1);
//printf("Mutex unlock\n");
} else if(control == 3) {
pthread_mutex_lock(&thread1);
//printf("Mutex lock\n");
d = getc(drive4);
printf("%c\n", (char)d);
control++;
pthread_mutex_unlock(&thread1);
//printf("Mutex unlock\n");
} else if(control == 4) {
pthread_mutex_lock(&thread1);
//printf("Mutex lock\n");
e = getc(drive5);
printf("%c\n", (char)e);
control = 0;
pthread_mutex_unlock(&thread1);
//printf("Mutex unlock\n");
}
}
}
int main() {
pthread_t th1;
pthread_create(&th1, NULL, processing, NULL);
pthread_join(th1, NULL);
}