Problem with multiple fopen() calls

Basically when hello.txt doesn't exist it does what its supposed to, however when hello.txt does exist, the program freezes- I mean i know its supposed to wait for hello.txt to be created, but in this case it doesn't respond if i create hello.txt after, nor does it print out the printf that's the first line of code. I've no idea why it does this- help? :s

[code]#include
#include
#include
#include

int main (void)
{
printf("hello");
int terminate=0;

FILE *file;
file=fopen("hello.txt", "r");

while (terminate != 1)
{
if (file==NULL)
{
terminate=1;
} else{
fclose(file);
file=fopen("hello.txt", "r");
}
}
fclose(file);
return 0;
} [/code]

Comments

  • : Basically when hello.txt doesn't exist it does what its supposed to,
    : however when hello.txt does exist, the program freezes- I mean i
    : know its supposed to wait for hello.txt to be created, but in this
    : case it doesn't respond if i create hello.txt after, nor does it
    : print out the printf that's the first line of code. I've no idea why
    : it does this- help? :s
    :
    : [code]: #include
    : #include
    : #include
    : #include
    :
    : int main (void)
    : {
    : printf("hello");
    : int terminate=0;
    :
    : FILE *file;
    : file=fopen("hello.txt", "r");
    :
    : while (terminate != 1)
    : {
    : if (file==NULL)
    : {
    : terminate=1;
    : } else{
    : [color=Red]fclose(file);
    : file=fopen("hello.txt", "r");[/color]
    : }
    : }
    : fclose(file);
    : return 0;
    : } [/code]:
    :
    [color=Blue]The RED part of the code never sets [b]terminate[/b] to 1 and the [italic]while[/italic] loops forever.

    I also have no idea what is the purpose of such code? Why close the file and then open it again on the next line?[/color]
  • I'm designing a SERVER/ PRODUCER/ CONSUMER thread, this is just the producer part. Basically its supposed to run the busy loop until the file no longer exists. My problem is, even if it is an infinite loop, shouldn't the first line of code be executed? That is the printf statement? For some reason it isn't/
  • [color=Blue]printf("hello"); must be visible if you ran that code - you say if file exists then "hello" is not printed? - I do not know, maybe it prints too fast and scrolls up and you do not see it?

    In any event, which OS you are writing that code for? I am confident that there are functions you can use to detect the file existence WITHOUT actually opening/closing it. Like findfirst(), etc.

    Also, between checks on file you probably should wait a few milliseconds. If you constantly is busy with the file and never give time to other threads to delete the file - then there may be an issue.

    Also, does file plays any real role or it is just a way to sync some processes? Because, there are more proper technologies for that, like mutex objects. If some process is busy with a disk resource - it creates a mutex of some name. Other process simply checks if mutex exists or not and that decides if disk resource is free to use or not yet.[/color]
  • it was never printed because you never flushed the output stream.

    place a '
    ' at the end of the printf to do this.

    is printf("hello
    ");

    alternatively use fflush();
  • This post has been deleted.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories