1

是否有任何方法/函数可以使用,int file = open(filepath, flag);因为我正在尝试实现flock并且因为ifstream file; file.open("filepath");无法使用flock()

我正在尝试学习如何flock()在 C++ 中实现,并且我在网上找到了一些示例。这是我写的:

  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
    }
  }

但现在我不知道如何处理文件。我想逐行获取内容。

这是我之前的做法:

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);

flock()但是当我使用打开文件时我不能使用,std::ifstream::open()因为flock()需要两个ints作为参数:

extern int flock (int __fd, int __operation) __THROW;

编辑:

这是我在@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);
    }
  }
}

它编译并运行,但std::cout<<"string[k]= "<<string[k]<<"\n";返回string[k]= ,仅此而已。

编辑2:

我有一个用 PHP 编写的网页,让用户能够输入 6 个值:

URL
URL Refresh Interval
Brightness
Color1 in hex
Color2 in hex
Color3 in hex

这些值将写入configuration.txt.

每次访问网页configuration.txt时,PHP 都会从那里获取一些值,然后将其关闭。 configuration.txt当提交一个或多个上述值然后关闭时,它也会打开。

接下来,我有一个 bash,它定期wgets将 URL 来自configuration.txt并将输出写入另一个文件,称为url_response.txt.

while [ 0 ]
do
    line=$(head -n 1 data/configuration.txt)
    wget -q -i $line -O url_response.txt
    sleep 2
done

该脚本将被放入 C++ 程序中。

最后,同一个 C++ 程序必须访问url_response.txt以从中获取和解析一些字符串,并且还必须访问configuration.txt以从中获取三种颜色。

所以我想flock()在所有这些程序中实现。

一切都将在 Linux raspberrypi 4.19.58-v7l+ 上运行的 Raspberry Pi 4 上运行。

4

1 回答 1

2

我认为可以使用 C API 轻松完成。fopen()对从 C: 、fclose()等继承的文件 IO 使用函数族fgets()

开始的简单文件读取示例(并链接到文档)。只需在需要的地方添加您查找的例程即可。

#include <cstdio>
#include <cstdlib>

int main()
{
    FILE* fp = std::fopen("test.txt", "r");
    if(!fp) {
        std::perror("File opening failed");
        return EXIT_FAILURE;
    }

    int c; // note: int, not char, required to handle EOF
    while ((c = std::fgetc(fp)) != EOF) { // standard C I/O file reading loop
       std::putchar(c);
    }

    if (std::ferror(fp))
        std::puts("I/O error when reading");
    else if (std::feof(fp))
        std::puts("End of file reached successfully");

    std::fclose(fp);
}
于 2019-09-05T11:23:55.263 回答