gpt4 book ai didi

c++ - 将 Linux 打开、读取、写入、关闭功能转换为在 Windows 上工作

转载 作者:可可西里 更新时间:2023-11-01 09:47:09 24 4
gpt4 key购买 nike

下面的代码是为 Linux 编写的,使用了打开、读取、写入和关闭。我在 Windows 计算机上工作,我通常在其中使用 fopen、fgets、fputs、fclose。现在,我收到打开、读取、写入和关闭的无原型(prototype)错误。是否有我可以包含的头文件以使其在 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);
}

最佳答案

Microsoft 直接支持 POSIX 风格的低级 IO 调用,例如 open() , read() , , write() , 和 close() ;尽管似乎带有误导性的“已弃用”特征。

所需的 header 是 <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 风格的等效名称。

关于c++ - 将 Linux 打开、读取、写入、关闭功能转换为在 Windows 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com