【问题标题】:Replacing C code written in win32 into linux based system将用win32编写的C代码替换为基于linux的系统
【发布时间】:2018-05-11 09:06:33
【问题描述】:

我正在尝试为 MacOSX 复制 WIN32 代码,但是我无法复制部分代码

这是代码。

main_int64 pos;
  OP_ASSERT(sizeof(pos)==sizeof(fpos_t));
  /*Translate the seek to an absolute one.*/
  if(_whence==SEEK_CUR)
  {
    int ret;
    ret=fgetpos((FILE *)_stream,(fpos_t *)&pos);
    if(ret)return ret;
  }
  else if(_whence==SEEK_END)pos=_filelengthi64(_fileno((FILE *)_stream));
  else if(_whence==SEEK_SET)pos=0;
  else return -1;
  /*Check for errors or overflow.*/
  if(pos<0||_offset<-pos||_offset>OP_INT64_MAX-pos)return -1;
  pos+=_offset;

  int seeko = fsetpos((FILE *)_stream,(fpos_t *)&pos);
  fprintf("BaseTool", "SEEKO VALUE %d \n", seeko);
  return seeko;

我被困在了

否则if(_whence==SEEK_END)pos=_filelengthi64(_fileno((FILE *)_stream));

我不确定如何在基于 mac 的系统中替换这些函数,因为这些是基于 WIN 的函数

【问题讨论】:

  • MacOSX 不是 Linux,但两者(几乎)都是 POSIX。请参阅this 对几乎重复的问题的回答

标签: c macos


【解决方案1】:

从代码中我了解到您正在尝试获取文件的长度,在 linux 中您可以这样做,

 int fp           = open("check5.c", O_RDONLY);
off_t lengthOfFile = lseek(fp, 0, SEEK_END);
close(fp);
printf("%d",lengthOfFile);

所以用“lseek(fileno,0, SEEK_END)”替换它应该给出文件的长度。

【讨论】:

  • 我建议改用stat(2)
  • 是的 stat 应该也可以。但是对于这个 lseek 也应该工作
  • 如果check5.c 不是普通文件,但例如一个先进先出...如果check5.c 被其他进程同时写入,你的lseek 技巧会产生奇怪的结果。
猜你喜欢
  • 2010-12-15
  • 2017-03-10
  • 2014-01-23
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多