fscanf c++ equivalent

fscanf c++ equivalent

本文关键字:equivalent c++ fscanf      更新时间:2023-10-16

我想将以下 C 代码翻译成 C++。

FILE *fp = NULL;
fp = fopen("./filename", "r");
int i = 0;
fscanf(fp, "%dn", &i);
uint16_t j = (uint16_t) i;

这就是我想出的:

  ifstream file;
  string filename = "./filename";
  file.open(filename.c_str(), ios::in);
  errno = 0;
  if (file.fail()) {
      int tmp = errno;
      std::cout << file.c_str () << " not found: strerror(" << tmp << "): " << strerror(tmp) );
  }
  int i = 0;
  file >> i >> std::endl;       
  uint16_t j = (uint16_t) i;

我想知道语法是否正确或可改进,更重要的是它对各种输入是否安全。

int read_int(const std::string file_name) {
    std::ifstream file(file_name); //the file will close itself on destruction
    std::uint16_t i;
    //extract type, don't worry about what it is it will either compile or not
    if(!(file >> i)) { //Catch failure
         //or however you wish to deal with it.
         throw std::runtime_error("can't read file");
    }
    return i;
}
int main() {
    try{
        std::uint16_t i=read_int("./filepath");
        //do with i...
    }
    catch(const std::exception& e) {
         std::cerr << e.what() << std::endl;
         return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

请注意,如果您没有 C++11,则需要使用 c_str() 打开文件,但首选字符串方法。

编辑:fstream关闭自己,没有必要自己关闭它,功能就在那里,以防您必须这样做,但是依靠RAII语义要好得多:

http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

RAII 规定您应该在构造时打开文件,它将在销毁时关闭,这可确保没有任何无效(不包括 EOF,找不到文件...)fstream 对象防止错误。RAII是C++的基本结构,应该在涉及资源的地方使用。

流析构函数的文档在这里:

http://en.cppreference.com/w/cpp/io/basic_fstream

销毁basic_fstream和关联的缓冲区,关闭文件

完全等价的是:

std::ifstream fs( "./filename" );
int i = 0;
fs >> i >> std::ws;
uint16_t j = i;

这是否是你真正想要的则是另一个问题:使用 fscanf格式字符串中的"n"表明(至少对我来说)是这样你真的想读一个'n',而不是任意的空格;然而,"n"fscanf中的意思是跳到下一个非空格。 (在交互式输入的情况下,这可能是一个真实的问题,因为你不会从你的scanf回来——或者我的上面的替换 - 直到遇到非空格字符或文件结尾。 对于来自文件的输入,它可能不是问题。

读取面向行的输入时,经典的解决方案是使用 std::getline,然后std::istringstream来解析它。