Are there any methods/functions that can I use with int file = open(filepath, flag);
because I am trying to implement flock
and because ifstream file; file.open("filepath");
won't work with flock()
?
I am trying to learn how to implement flock()
in C++ and I have found some examples online. This is what I've written:
int file;
char configPath[] = "data/configuration.txt";
file = open(configPath, O_RDONLY);
if(file != -1) {
std::cout<<"Successfully opened file: "<<configPath<<std::endl;
if(flock(file,1) == 0) {
//do things with the file
}
}
But now I don't know how to do things with the file. I would like to get the content line by line.
Here is how I did it before:
int readConfig(std::ifstream& configFile, std::string (&string)[10], int counter) {
std::cout<<"void readConfiguration\n";
if(!configFile) {
std::cout<<"configuration.txt couldn't be opened\n";
} else {
// Get content line by line of txt file
int i = 0;
while(getline(configFile,string[i++]));
//for debug only
for(int k = 0; k<i; k++) std::cout<<"string[k]= "<<string[k]<<"\n";
}
return counter;
}
configFile.open("data/configuration.txt");
std::string inputs[10];
int counter;
readConfig(configFile, inputs, counter);
But I can't use flock()
when I open the file using std::ifstream::open()
because flock()
takes two ints
as arguments:
extern int flock (int __fd, int __operation) __THROW;
EDIT:
This is what I came up with help from @MSalters :
int readConfig(std::istream& configFile, std::string (&string)[10], int counter) {
std::cout<<"void readConfiguration\n";
if(!configFile) {
std::cout<<"configuration.txt couldn't be opened\n";
} else {
// Get content line by line of txt file
int i = 0;
while(getline(configFile, string[i++]));
//for debug only
for(int k = 0; k<i; k++) std::cout<<"string[k]= "<<string[k]<<"\n";
counter = i;
}
return counter;
}
int main()
{
int file;
char configPath[] = "data/configuration.txt";
file = open(configPath, O_RDONLY);
if(file != -1) {
std::cout<<"Successfully opened file: "<<configPath<<std::endl;
if(flock(file,1) == 0) {
__gnu_cxx::stdio_filebuf<char> fd_file_buf{file, std::ios_base::out | std::ios_base::binary};
std::istream fd_stream{&fd_file_buf};
std::string inputs[10];
int counter;
readConfig(fd_stream, inputs, counter);
flock(file,8);
file = close(file);
}
}
}
It compiles and runs, but std::cout<<"string[k]= "<<string[k]<<"\n";
returns string[k]=
and that's it.
EDIT2:
I have a web-page written in PHP that gives the user the ability to input 6 values:
URL
URL Refresh Interval
Brightness
Color1 in hex
Color2 in hex
Color3 in hex
These values will be written in configuration.txt
.
Each time the web-page is accessed configuration.txt
gets opened, the PHP gets some values from there and then closes it.
configuration.txt
is also opened when one or more of the above values are submitted and then it gets closed.
Next, I have a bash that regularly wgets
the URL from configuration.txt
and writes the output to a different file, called url_response.txt
.
while [ 0 ]
do
line=$(head -n 1 data/configuration.txt)
wget -q -i $line -O url_response.txt
sleep 2
done
This script will be put inside a C++ program.
Finally, the same C++ program will have to access url_response.txt
to get and parse some strings from it and it will also have to access configuration.txt
to get the three colors from it.
So I want to implement flock()
in all of these programs.
Everything will run on a Raspberry Pi 4 that runs on Linux raspberrypi 4.19.58-v7l+.