I'm very stuck with one single problem, I need prinft file's line one by one in order with one thread. This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
/* Prototypes */
pthread_t reader;
void *readLine(void *line)
{
printf("Thread: %s \n",(char*)line);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("texto.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
if(pthread_create(&reader, NULL, readLine, (void *)line)) {
fprintf(stderr, "Error creating thread\n");
}
}
fclose(fp);
if (line)
free(line);
exit(EXIT_SUCCESS);
}
This code print lines in disorder and no all thread are print the line passed by readLine, how can I print line by line by the same thread reader?