转换Linux打开,读取,写入,关闭功能以在Windows上工作

时间:2018-02-27 04:47:17

标签: c++ c windows posix c++builder

下面的代码是为Linux编写的,使用open,read,write和close。我正在使用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);
}

2 个答案:

答案 0 :(得分:3)

Windows中的相应功能使用相同的名称,但名称前加下划线(_)。

open - &gt; _open
close - &gt; _close

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

答案 1 :(得分:2)

Microsoft直接支持POSIX风格的低级IO调用,例如open()read()write()close();虽然看起来有误导性,但已弃用&#34;表征

所需标头为<io.h>

调用对应于以前面的下划线命名的函数,因此open()映射到_open()

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

低级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

某些低级函数可能没有非下划线,POSIX样式的等效名称。