c++ - programming c threaded io -
hi want read text file text , write out file. need threaded 3 party can run @ same time. trying use buffer read in , buffer write out cant work out.
my code :
#include <stdio.h> #include <pthread.h> char inbuf[1000]; //char outbuf[]; void *readerfun(void *meg){ char *inputfile; file *input_ptr; inputfile = (char *) meg; input_ptr = fopen(inputfile, "r"); if(input_ptr == null){ printf("%s\n", inputfile); printf("input file not working\n"); return; } while(!feof(input_ptr)){ fscanf(input_ptr, "%s", &inbuf); sleep(1); // printf("%s\n", &inbuf ); // printf("test _____________\n" ); } return null; } void *modifierfun(void *meg){ //char c; //while((c = getc(inbuf) != eof)){ //strcat(outbuf, c); //} //} return null; } void *writerfun(void *meg){ char *outfile; file *output_ptr; outfile = (char *) meg; output_ptr = fopen(outfile, "a"); while(inbuf != null){ printf("%s\n", inbuf ); fprintf(output_ptr, "%s", inbuf); } return null; } int main (int argc, char **argv){ pthread_t reader, modifier, writer; char *meg = argv[1]; char *meg2 = argv[2]; int ret1, ret2, ret3; ret1 = pthread_create(&reader, null, readerfun, (void *) meg); ret2 = pthread_create(&modifier, null, modifierfun, (void *) meg); ret3 = pthread_create(&writer, null, writerfun, (void *) meg2); pthread_join(reader, null); pthread_join(modifier, null); pthread_join(writer, null); return 0; }
would big point me down right road. have looked on net , cant find im looking for.
Comments
Post a Comment