Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我可以对一个'int file=open(path,flag);'做些什么吗?_C++ - Fatal编程技术网

C++ 我可以对一个'int file=open(path,flag);'做些什么吗?

C++ 我可以对一个'int file=open(path,flag);'做些什么吗?,c++,C++,是否有任何方法/函数可用于int file=open(filepath,flag)因为我正在尝试实现flock和ifstream文件;打开(“文件路径”)不能与flock()一起使用。 我试图在C++中学习如何实现 fCube()/,我在网上找到了一些例子。这是我写的: int file; char configPath[] = "data/configuration.txt"; file = open(configPath, O_RDONLY); if(fi

是否有任何方法/函数可用于
int file=open(filepath,flag)因为我正在尝试实现
flock
ifstream文件;打开(“文件路径”)不能与
flock()一起使用。

我试图在C++中学习如何实现<代码> fCube()/<代码>,我在网上找到了一些例子。这是我写的:

  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
    }
  }
编辑: 这就是我在@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);
    }
  }
}
这些值将写入
configuration.txt

while [ 0 ]
do
    line=$(head -n 1 data/configuration.txt)
    wget -q -i $line -O url_response.txt
    sleep 2
done
每次访问
configuration.txt
网页时,PHP都会从中获取一些值,然后将其关闭。
configuration.txt
在提交上述一个或多个值后关闭时也会打开

接下来,我有一个bash,它定期从
configuration.txt
获取URL,并将输出写入另一个名为
URL\u 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++程序必须访问<代码> URLYRealth.TXT < /Cord>,以获取和解析它的一些字符串,并且它也必须访问<代码>配置。TXT < /代码>从中获得三种颜色。 所以我想在所有这些程序中实现

flock()


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

我认为使用C API可以轻松完成。 对从C继承的文件IO使用函数系列:
fopen()
fclose()
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);
}
#包括
#包括
int main()
{
文件*fp=std::fopen(“test.txt”、“r”);
如果(!fp){
std::perror(“文件打开失败”);
返回退出失败;
}
int c;//注意:处理EOF需要int,而不是char
while((c=std::fgetc(fp))!=EOF){//标准c I/O文件读取循环
std::putchar(c);
}
如果(标准:铁磁(fp))
std::puts(“读取时的I/O错误”);
否则如果(标准::feof(fp))
std::put(“成功到达文件结尾”);
std::fclose(fp);
}

使用“常规”C文件界面?@molbdnilo如果我知道怎么做,甚至知道我不会在这里发布的内容,是吗?我正在网上阅读我需要什么和我能用什么!但我没有大局,也没有知识。我正在进行学习。以下是您可以使用
int文件
:@acraig5075:no,这是一个C风格的
文件*
。这是一个POSIX <代码> INTFD < /COD>。这意味着我需要在这里阅读:并且,简单地说,C API通常是在实现自己的低级例程以从C++ STL上替换时所需要的。它还有很长的路要走。文件锁可以通过文件锁表(存储在当前目录的锁文件中)实现。如果是这样,您应该在打开文件之前放置flock()(而flock()反过来将封装文件锁表的打开和读取)。无论如何,在工作文件IO之前,检查锁是否空闲(如果是,则锁定)是否会被释放。@bleah1您自己承认您知道的不多,这很好,但如果您按照患者的建议尝试C例程,您似乎知道“事情会变得很困难”。这似乎是一个经验丰富的开发人员意见。遗憾的是,@ BLASH1C可能很难,但是IHO比C++更简单更干净,它倾向于把所有的特征都包含在太阳底下,使语法变得如此复杂和难以读懂!我已经对它进行了编辑并添加了所有细节。
while [ 0 ]
do
    line=$(head -n 1 data/configuration.txt)
    wget -q -i $line -O url_response.txt
    sleep 2
done
#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);
}