检查文本文件中的 0 或 1,并根据结果返回

check a text file for 0 or 1 and return based on result?

本文关键字:返回 结果 文件 文本 检查      更新时间:2023-10-16

我需要帮助检查存储在计算机上的文本文件中的字符串。 伪示例

if String = x {
    Sleep(100)
}
else {
    exit(0)
}

我只是想检查文本文件中的一行文本,然后根据该行返回一个值......例如,如果字符串continue那么我希望它继续exit(0).

我的C++项目是一个 dll。基本上我要做的是打开一个与dll位于同一位置的文件,检查它是否01,如果它返回1则继续该过程0终止它。

经过一番来回,我相信这就是你想要的

#include <iostream>
#include <fstream>
int main() {
  // you probably just want "test.txt" here not "c:\test.txt"
  std::ifstream data_store("c:\test.txt");
  if (!data_store.good()) {
    std::cerr << "couldn't open filen";
    return 1;
  }
  int contents = 0;
  data_store >> contents;
  if (contents == 0) {
    return 0; // or std::exit(0) if not in main
  } else if (contents == 1) {
    // do the rest of the code here
  }
}