Linux下的一个程序如何改写成vc++6.0的呢
时间:2011-01-08
来源:互联网
C/C++ code
我用vc改写部分如下:
C/C++ code
class FileLock { public: FileLock(); ~FileLock(); public: void Open(const string& fileName); void Close(); void LockShared(); void LockExclusive(); void Unlock(); off_t GetSize(); protected: int m_fd; string m_fileName; }; FileLock::FileLock() : m_fd(-1) { } FileLock::~FileLock() { if (m_fd) { close(m_fd); } } void FileLock::Open(const string& fileName) { if (m_fd) close(m_fd); m_fd = open(fileName.c_str(), O_RDWR | O_CREAT, 0666); if (m_fd < 0) { int p = fileName.rfind('/'); if ((errno == ENOENT) && (p > 0)) { MakeDirs(fileName.substr(0, p)); m_fd = open(fileName.c_str(), O_RDWR | O_CREAT, 0666); if (m_fd < 0) throw SystemException("Unable to open file '%s', open failed with '%s'.", fileName.c_str(), ErrnoToString(errno).c_str()); } else throw SystemException("Unable to open file '%s', open failed with '%s'.", fileName.c_str(), ErrnoToString(errno).c_str()); } m_fileName = fileName; } void FileLock::Close() { if (m_fd) { close(m_fd); m_fileName.clear(); } } void FileLock::LockShared() { if (flock(m_fd, LOCK_SH)) throw SystemException("Unable to lock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str()); } void FileLock::LockExclusive() { if (flock(m_fd, LOCK_EX)) throw SystemException("Unable to lock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str()); } void FileLock::Unlock() { if (flock(m_fd, LOCK_UN)) throw SystemException("Unable to unlock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str()); } off_t FileLock::GetSize() { struct stat st; if (fstat(m_fd, &st)) throw SystemException("Unable to stat file '%s', fstat failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str()); return st.st_size; }
我用vc改写部分如下:
C/C++ code
void FileLock::Open(const string& fileName) { if (m_fd) _close(m_fd); m_fd = _open(fileName.c_str(), _O_RDWR | _O_CREAT);//, 0666); if (m_fd < 0) { int p = fileName.rfind('/'); if ((errno == ENOENT) && (p > 0)) { MakeDirs(fileName.substr(0, p)); m_fd = _open(fileName.c_str(), O_RDWR | O_CREAT);//, 0666); if (m_fd < 0) throw SystemException("Unable to open file '%s', open failed with '%s'.", fileName.c_str(), ErrnoToString(errno).c_str()); } else throw SystemException("Unable to open file '%s', open failed with '%s'.", fileName.c_str(), ErrnoToString(errno).c_str()); } m_fileName = fileName; } void FileLock::Close() { if (m_fd) { _close(m_fd); //m_fileName.clear(); } } //这个不会改 void FileLock::LockShared() {//LOCK_SH (共享鎖定)、 LOCK_EX (互斥鎖定) if (flock(m_fd, LOCK_SH)) //lock throw SystemException("Unable to lock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str()); } void FileLock::LockExclusive() {//这个不会改 if (flock(m_fd, LOCK_EX)) throw SystemException("Unable to lock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str()); } void FileLock::Unlock() { // if (flock(m_fd, LOCK_UN)) throw SystemException("Unable to unlock file '%s', flock failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str()); } off_t FileLock::GetSize() { struct _stat st; //int fstat(int filedes, struct stat *buf); i if (_fstat(m_fd, &st))// fstat function returns information about an open file. throw SystemException("Unable to stat file '%s', fstat failed with '%s'.", m_fileName.c_str(), ErrnoToString(errno).c_str()); return st.st_size; // 总大小,字节为单位 }
作者: gisupc 发布时间: 2011-01-08
说白了就是求高人指点如何将 if (flock(m_fd, LOCK_EX))
转换为windows下vc6.0程序,
谢谢!
转换为windows下vc6.0程序,
谢谢!
作者: gisupc 发布时间: 2011-01-08
就是一个线程同步的问题,在windows有很多线程同步的方法啊,事件、信号量……
作者: miaolingshaohua 发布时间: 2011-01-08
引用 2 楼 miaolingshaohua 的回复:
就是一个线程同步的问题,在windows有很多线程同步的方法啊,事件、信号量……
就是一个线程同步的问题,在windows有很多线程同步的方法啊,事件、信号量……
具体这个,能给点具体的意见吗?谢谢!
作者: gisupc 发布时间: 2011-01-08
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28