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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14575068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:27:56  来源:igfitidea点击:

close function not being recognized - C++

c++cunix

提问by cybertextron

I have the following piece of code in a file called RaftLog.cc:

我在一个名为的文件中有以下一段代码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.

However, when I compile I have:

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

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,

I don't know why the close()function does not get included by #include <fcntl.h>. May someone please help me? Also let me know if I should include more code.

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

回答by billz

You need to include unistd.hheader file if you call closefunction

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

#include <unistd.h>

Use fopen/fclose, fdopen/closepair

使用fopen/fclose,fdopen/close配对

回答by Mo0gles

In Visual Studio 2015

在 Visual Studio 2015 中

#include <corecrt_io.h>

seem to work!

似乎工作!