Link to home
Start Free TrialLog in
Avatar of LizaMoly
LizaMoly

asked on

C++ Waiting for a file function

I'm working for a c++ project using Ubuntu 14.
In some parts of the project in need to copy some files from one program to another. The receiving function wait for the file to be copied and return the value of its contents.  I created such function but it make a problem because after it validated the file is existed it read another time and the content of the file deleted.

inline bool exists_test (const std::string& name) {
  struct stat buffer;   
  return (stat (name.c_str(), &buffer) == 0); 
}
string waitResponse(string filename, string msg)
{

    ifstream inUserID(filename.c_str());
    if (!exists_test(filename)){
        
        cerr<< msg <<endl;
        sleep(2);
        cout << " file not found " << endl;
        waitResponse(filename, msg);
        
    } 
        sleep (2);
        cout <<  "file found -----  " << endl ;
         string fileCont;
         inUserID >> fileCont;
         cout  << "the file content is   " << fileCont << endl; 
         return fileCont;
    
}

Open in new window


The call is :  
string userpass;
       userpass = waitResponse("data/users.txt" , " waiting for users authentication");

Open in new window


What is the solution for this problem ?
Is there any easier way to do that ?
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
From what you've shown us so far, you don't have an adequate method for asynchronous inter-process communication. One process creates the file, a different process reads it, and who then deletes the file? It's possible (sometimes necessary) to create IPC based on rendezvous in the file system, but so far we don't know enough about what you're trying to do to give more specific advice.

One alternative would be to use a named pipe. If there's just one producer and one consumer, this would be very straightforward, especially if it's okay for the consumer to stall waiting for input.
Hi,

I have seen the code for waitResponse which is written in following way:

string waitResponse(string filename, string msg)
{
..
sleep(2)
..
waitResponse(filename, msg);
..
}

If you comment the sleep line, the program will receive SIGSEGV signal since function waitResponse is calling the same function infinitely if that file is not found.
If you comment sleep line and compile using
g++ -g -Wall source.cpp
ulimit -c unlimited
a.out

Open in new window

a.out will create core file.


Hence
Replace:
if (!exists_test(filename))
{
        cerr<< msg <<endl;
        sleep(2);
        cout << " file not found " << endl;
        waitResponse(filename, msg);
}

Open in new window

With:
while (!exists_test(filename))
{
        cerr<< msg <<endl;
        sleep(2);
        cout << " file not found " << endl;
}

Open in new window


Also do the change proposed by sarabande for reading all content of the file.