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.
The call is :
What is the solution for this problem ?
Is there any easier way to do that ?
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;
}
The call is :
string userpass;
userpass = waitResponse("data/users.txt" , " waiting for users authentication");
What is the solution for this problem ?
Is there any easier way to do that ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Hence
Replace:
Also do the change proposed by sarabande for reading all content of the file.
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
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);
}
With:while (!exists_test(filename))
{
cerr<< msg <<endl;
sleep(2);
cout << " file not found " << endl;
}
Also do the change proposed by sarabande for reading all content of the file.
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.