fopen problem

This is a discussion on fopen problem within the C Programming forums, part of the General Programming Boards category; Hi.. I am trying to use a thread to continuersly read data from a file while another process write data ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    46

    fopen problem

    Hi..

    I am trying to use a thread to continuersly read data from a file while another process write data to the file. It is not important that the same data is read two times before changed, or changed two times before read.
    My problem is that I read the file once correct but after that it cannot open the file correct, and I have remembered to close the file before the new opening..
    This is part of the code:
    Code:
             FILE *persondetection = NULL;
             while(1) {
                    persondetection = fopen(strcat(loadDir,"personscan.txt"), "r");
    		// if file not opened successfully then try again
    		while (persondetection==NULL) {
    		printf("lasercontrol: - File not okay, trying again\n");
    		sleep(1);
    		persondetection = fopen(strcat(loadDir,"personscan.txt"), "r");
    		}
            fclose (persondetection);
            }
    What am I missing..?

    the output is ->
    .... lasercontrol: - File not okay, trying again
    lasercontrol: - File not okay, trying again
    lasercontrol: - File not okay, trying again
    lasercontrol: - File not okay, trying again
    .....
    Last edited by sniper83; 07-21-2007 at 11:08 AM.

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    46
    Okay.. Now I have come a litte closer to the problem.. It happens whenever it is open the file the second time. eventhough it has not changed since last opening.. It seems like the file is still locked or so, but have closed it.. :s
    Any idears at all..?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > strcat(loadDir,"personscan.txt")
    I wonder how long this string is going to get inside your while(1) loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    58
    strcat doesn't return the string.
    It adds the second to the first

    do something like

    Code:
    char* t;
    t=malloc(strlen(loadDir)+strlen("personscan.txt")+1);
    strcpy(t,loadDir);
    strcat(t,"personscan.txt");
    
    fopen(t,"r");

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    46
    Quote Originally Posted by Salem View Post
    > strcat(loadDir,"personscan.txt")
    I wonder how long this string is going to get inside your while(1) loop.
    Damn.. Ofcause... Good observed.. And I have focused to clean the used file, and releasing the file.. I have not even seen the strcat command.. I must have stared me bblind of that one..

    Thanks a lot..

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    46
    Quote Originally Posted by Govalant View Post
    strcat doesn't return the string.
    It adds the second to the first

    do something like

    Code:
    char* t;
    t=malloc(strlen(loadDir)+strlen("personscan.txt")+1);
    strcpy(t,loadDir);
    strcat(t,"personscan.txt");
    
    fopen(t,"r");
    Thanks, god observed..

    Copy/paste problem.. What i wanted was not exactly that but simply:

    Code:
    FILE *persondetection = NULL;
            strcat(loadDir,"personscan.txt"); EDIT 
            while(1) {
                    persondetection = fopen(loadDir), "r");
    		// if file not opened successfully then try again
    		while (persondetection==NULL) {
    		   printf("lasercontrol: - File not okay, trying again\n");
    		   sleep(1);
    		   persondetection = fopen(loadDir), "r");
    		}
            fclose (persondetection);
            }
    Last edited by sniper83; 07-21-2007 at 12:03 PM.

  7. #7
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    324
    Quote Originally Posted by sniper83 View Post
    Hi..

    I am trying to use a thread to continuersly read data from a file while another process write data to the file. It is not important that the same data is read two times before changed, or changed two times before read.
    My problem is that I read the file once correct but after that it cannot open the file correct, and I have remembered to close the file before the new opening..
    This is part of the code:
    Code:
             FILE *persondetection = NULL;
             while(1) {
                    persondetection = fopen(strcat(loadDir,"personscan.txt"), "r");
    		// if file not opened successfully then try again
    		while (persondetection==NULL) {
    		printf("lasercontrol: - File not okay, trying again\n");
    		sleep(1);
    		persondetection = fopen(strcat(loadDir,"personscan.txt"), "r");
    		}
            fclose (persondetection);
            }
    What am I missing..?

    the output is ->
    .... lasercontrol: - File not okay, trying again
    lasercontrol: - File not okay, trying again
    lasercontrol: - File not okay, trying again
    lasercontrol: - File not okay, trying again
    .....
    This code is an infinite loop.

    I see no conditional or even a break statement or even a goto here. Or even a function call that does another subroutine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21