1

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.

Masquerade
  • 3,580
  • 5
  • 20
  • 37

1 Answers1

1

TL;DR - you are running the reading before the writing

The character you're reading is probably the EOF character which is equal to -1 (an integer). I tried running these programs one after another using Python 3 and Python2, compiling C code with both g++ and gcc.

I was only able to reproduce your results when the first program was reading before the second one was able to write anything - hence the EOF. I also did a printf("%c", EOF) resulting in the same character. YES, I even did a if (ch == EOF) printf("gotcha"); after the character reading.

Important:

If you run these programs side by side think about buffering. When one program writes something to a file it doesn't have to. It's stored in a buffer and if the buffer is full - then it writes the content of the buffer to the filie - that's because file access is expensive. So basically the characters you're attempting to read are probably still in the buffer.

Community
  • 1
  • 1
Adrian Jałoszewski
  • 1,695
  • 3
  • 17
  • 33
  • http://stackoverflow.com/questions/18984092/python-2-7-write-to-file-instantly - about instant writing – Adrian Jałoszewski Apr 29 '17 at 10:33
  • I'll try to run this program you mentioned the link of but i also need a way to instantly read input in Python or C from the text file. – Masquerade Apr 29 '17 at 10:45
  • are you using python2 or python3? if you're using python2, then you can pass 0 as the third argument to the `open` function, else you have to look at this http://stackoverflow.com/questions/27067713/why-text-i-o-must-be-buffered-in-python-3 – Adrian Jałoszewski Apr 29 '17 at 10:56
  • Problem solved on adding 0 as third argument, input and output both works fine – Masquerade Apr 29 '17 at 16:30