转换 Linux 打开、读取、写入、关闭函数以在 Windows 上工作

Convert the Linux open, read, write, close functions to work on Windows

下面的代码是为 Linux 编写的,使用了打开、读取、写入和关闭。我正在 Windows 计算机上工作,我通常在其中使用 fopen、fgets、fputs、fclose。现在,我收到打开、读取、写入和关闭的无原型错误。是否有我可以包含的头文件以使其在 Windows 计算机上运行,​​或者我是否需要转换代码?你能展示如何转换它以便它在 Windows 上工作相同,或者至少让我指向一个显示如何转换它的在线文档吗?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifdef unix
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifndef O_BINARY
#define O_BINARY 0
#endif

#define NB 8192
char buff[NB];

int
main(argc,argv)
int argc;
char **argv;
{
int fdi, fdo, i, n, m;
char *p, *q;
char c;


if( argc > 0 )
    printf( "%s:  Reverse bytes in 8-byte values \n", argv[0] );
if( argc > 1 )
    strcpy( buff, argv[1] );
else
    {
    printf( "Input file name ? " );
    gets( buff );
    }
fdi = open( buff, O_BINARY | O_RDONLY, S_IREAD );

if( fdi <= 0 )
    {
    printf( "Can't open <%s>\n", buff );
    exit(2);
    }

if( argc > 2 )
    strcpy( buff, argv[2] );
else
    {
    printf( "Output file name ? " );
    gets( buff );
    }
fdo = open( buff, O_BINARY | O_RDWR | O_CREAT | O_TRUNC,
      S_IREAD | S_IWRITE );
if( fdo <= 0 )
    {
    printf( "Can't open <%s>\n", buff );
    exit(2);
    }

while( (n = read( fdi, buff, NB )) > 0 )
    {
    m = n / 8;
    p = buff;
    q = buff+7;
    for( i=0; i<m; i++ )
        {
        c = *p;
        *p++ = *q;
        *q-- = c;
        c = *p;
        *p++ = *q;
        *q-- = c;
        c = *p;
        *p++ = *q;
        *q-- = c;
        c = *p;
        *p++ = *q;
        *q-- = c;
        p += 4;
        q += 12;
        }
    write( fdo, buff, n );
    }
close( fdo );
close( fdi );
exit(0);
}

Windows 中的相应函数使用相同的名称,但在名称前加上下划线 (_)。

open -> _open
close -> _close

等等

它们在 header io.h 中声明。有关所有受支持函数的列表,请参阅 https://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx

Borland C++ Builder 将二进制文件访问函数封装到:

FileOpen
FileCreate
FileRead
FileWrite
FileSeek
FileClose

这里是加载文本文件的简单示例:

BYTE *txt=NULL; int hnd=-1,siz=0;
hnd = FileOpen("textfile.txt",fmOpenRead);
if (hnd!=-1)
 {
 siz=FileSeek(hnd,0,2);     // position to end of file (0 bytes from end) and store the offset to siz which means size of file
     FileSeek(hnd,0,0);     // position to start of file (0 bytes from start)
 txt = new BYTE[siz];
     FileRead(hnd,txt,siz); // read siz bytes to txt buffer
     FileClose(hnd);
 }
if (txt!=NULL)
 {
 // here do your stuff with txt[siz] I save it to another file
 hnd = FileCreate("output.txt");
 if (hnd!=-1)
  {
  FileWrite(hnd,txt,siz);   // write siz bytes to txt buffer
  FileClose(hnd);
  }
 delete[] txt;
 }

IIRC 所有这些都是 VCL 的一部分,因此如果您使用的是控制台,则需要在项目创建期间设置 VCL 包含检查或手动添加。

微软直接支持POSIX-stylelow-level等IO调用open(), read(), , write(), and close();尽管似乎具有误导性 "deprecated" 特征。

要求的header是<io.h>

这些调用对应于以前面的下划线命名的函数,因此 open() maps to _open()

The full list of supported "low-level" IO functions Microsoft supports are:

Low-Level I/O

Low-Level I/O Functions

Function                                Use
_close                                  Close file
_commit                                 Flush file to disk
_creat, _wcreat                         Create file
_dup                                    Return next available file descriptor for given file
_dup2                                   Create second descriptor for given file
_eof                                    Test for end of file
_lseek, _lseeki64                       Reposition file pointer to given location
_open, _wopen                           Open file
_read                                   Read data from file
_sopen, _wsopen, _sopen_s, _wsopen_s    Open file for file sharing
_tell, _telli64                         Get current file-pointer position
_umask, _umask_s                        Set file-permission mask
_write                                  Write data to file

某些 low-level 函数可能没有 non-underscore、POSIX-style 等效名称。