无法识别关闭函数 - C++

close function not being recognized - C++

本文关键字:函数 C++ 识别      更新时间:2023-10-16

我在一个名为RaftLog.cc的文件中有以下一段代码:

#include <algorithm>
#include <fcntl.h>
#include <sys/stat.h>

namespace LogCabin {
namespace Server {
namespace RaftConsensusInternal {
namespace FilesystemUtil = Storage::FilesystemUtil;
namespace {
bool
fileToProto(const std::string& path, google::protobuf::Message& out)
{
    int fd = open(path.c_str(), O_RDONLY);
    if (fd == -1)
        return false;
    else
        close(fd);
    FilesystemUtil::FileContents file(path);
        // more code down here, not useful to the problem.

但是,当我编译时,我有:

build/Server/RaftLog.cc: In function ‘bool LogCabin::Server::RaftConsensusInternal::{anonymous}::fileToProto(const string&, google::protobuf::Message&)’:
build/Server/RaftLog.cc:43:17: error: ‘close’ was not declared in this scope
build/Server/RaftLog.cc: In function ‘void LogCabin::Server::RaftConsensusInternal::{anonymous}::protoToFile(google::protobuf::Message&, const string&)’:
build/Server/RaftLog.cc:76:13: error: ‘close’ was not declared in this scope
In file included from ./Server/RaftConsensus.h:17:0,

我不知道为什么close()函数没有被#include <fcntl.h>包含。有人可以帮我吗?也让我知道我是否应该包含更多代码。

如果调用函数unistd.h则需要包含close头文件

#include <unistd.h>

使用fopen/fclosefdopen/close

在 Visual Studio 2015 中

#include <corecrt_io.h>

似乎有效!