I made a text file with only 2 characters in it ab
.Then i ran a program(name- pytho.py) in python to write characters in the text file in time intervals of 2 seconds.Then i ran another program(name- read.c) in C on the same text file simultaneously with the first program which reads and prints the data from the text file on the terminal.Somehow it is unable to read characters from the file.Moreover if i remove the printf statement printing 'loop running' nothing prints on the terminal, just the while loop keeps on running.The output of read.c on the terminal is-
started reading
loop running
aloop running
bloop running
�loop running
�loop running
�loop running
The file read.c is-
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *fp = fopen("inin.txt","r");
if(fp==NULL){
printf("file cannot be read\n");
}
int ch=0;
printf("started reading\n");
while(1){
printf("loop running\n");
ch=fgetc(fp);
printf("%c",ch);
sleep(3);
}
return 0;
}
The file pytho.py is -
import time
tar = open('inin.txt','a')
for i in range(0,10000):
tar.write('cd')
time.sleep(2)
print("data written\n")
Same problem occurs when i tried using another c program instead of python program to write characters into file.I used gcc for c program and python3 for python program.