Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

close function not being recognized - C++

Tags:

c++

c

unix

I have the following piece of code in a file called 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.

like image 584
cybertextron Avatar asked Jan 29 '13 03:01

cybertextron


1 Answers

You need to include unistd.h header file if you call close function

#include <unistd.h>

Use fopen/fclose, fdopen/close pair

like image 72
billz Avatar answered Oct 20 '22 09:10

billz