0

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);

}
Sai Peri
  • 339
  • 1
  • 3
  • 17
  • `sizeof(output)` is the size of `output`, not the size of memory that output might point to – M.M Oct 15 '18 at 22:05
  • Sai Peri, `strcat(output, (char)c);` should have generated compiler warnings. What compiler/warnings are you using? – chux - Reinstate Monica Oct 15 '18 at 22:36
  • (1) There are things called arrays; you can have an array of file streams; it will simplify your code. (2) [`while (!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). (3) If you have just one thread at work, you don't need mutexes etc. If you have multiple threads, you're going to need to rethink your coordination mechanisms. (4) Your code doesn't handle files of different sizes very well. (5) You need to know how big your array is so you can use it (reallocate it) safely. If you don't know how big it is, your code is broken. – Jonathan Leffler Oct 15 '18 at 22:57

0 Answers0