Creating a thread which writes into a file
I am learning threads and trying to implement a code which creates a
thread. The thread writes into a file. If the thread has been created it
returns 0 . The code here returns 0 but it does go into the function
write() but does not writes in the file . Just to check it goes in the
function i have put a printf() statement.I want the input should be taken
by command line here but it also does not work so to make it simpler i
have written only "hello world" to the file .
Here is the code :-
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *write(void *arg)
{
printf("HI \n");
FILE *fp;
fp = fopen("file.txt", "a");
if (fp == NULL) {
printf("error\n");
} else {
fprintf(fp, "hello world");
}
}
int main()
{
pthread_t thread;
int tid;
tid = pthread_create(&thread, NULL, write, NULL);
printf("thread1 return %d \n", tid);
exit(0);
}
No comments:
Post a Comment