【发布时间】:2018-08-06 15:59:30
【问题描述】:
下面的代码是为 Linux 编写的,使用 open、read、write 和 close。我正在使用通常使用 fopen、fgets、fputs、fclose 的 Windows 计算机上工作。现在,我在打开、读取、写入和关闭时收到无原型错误。是否有我可以包含的头文件以使其在 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);
}
【问题讨论】:
-
是的,转换代码。或者更好的是,在一开始就编写可移植的代码。随心所欲地去做。 / 请注意,要求场外资源的问题与 StackOverflow 无关。
-
我很确定我已经在 Windows 中使用过这些功能。只是包含正确的头文件的问题吗?
-
有各种适用于 Windows 的 unistd.h 实现,这是您唯一真正的问题。它基本上只是一些#includes 和一些#defines。
标签: c++ c windows posix c++builder