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 ?
LizaMolyAsked:
Who is Participating?
 
sarabandeCommented:
inUserID >> fileCont;
this statement only reads one single word from file. is that intended?

if not, to read all context you should do:

bool exists_test (const std::string& name, size_t & filesize) {
  struct stat buffer;   
  filesize = buffer.st_size;
  return (stat (name.c_str(), &buffer) == 0); 
}


size_t sizCont = 0;
if (!exists_test(filename, sizcont)){
        ...
}

//sleep (2);
std::ifstream inUserID(filename.c_str());

std::cout <<  "file found -----  " << endl ;
std::string fileCont(sizcont, '\0');
if (!inUserID.read(&fileCont[0], sizCont))
    // error
    return "";
inUserID.close();
std::cout  << "the file content is   " << fileCont << std::endl; 
return fileCont;

Open in new window


note, you need to close the file after read in case you want to open another file with the same name.

if the sender of the files copies the files faster than the receiver can read them, you would need to add a number or timestamp to the files. you would then do the same at the receiver side.

if the file only contains one word you may consider to either use other methods for passing the name to the receiver (for example by using a pipe or by udp) or copy an empty file to a specific folder and have the user id as filename.

Sara
0
 
jmcgOwnerCommented:
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.
0
 
Murugesan NagarajanCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.