转换Linux的打开,读取,写入,关闭功能以在Windows上运行 [英] Convert the Linux open, read, write, close functions to work on Windows

查看:99
本文介绍了转换Linux的打开,读取,写入,关闭功能以在Windows上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码是为Linux编写的,并使用打开,读取,写入和关闭.我在Windows计算机上工作,通常使用fopen,fgets,fputs,fclose.现在,我没有打开,读取,写入和关闭的原型错误.是否可以包含头文件以使其在Windows计算机上工作,还是需要转换代码?您能否显示如何进行转换,使其在Windows上也能正常工作,或者至少将我指向显示其转换方式的在线文档?

The code below was written for Linux and uses open, read, write and close. I am working on a Windows computer where I normally use fopen, fgets, fputs, fclose. Right now I get a no prototype error for open, read, write and close. Is there a header file I can include to make this work on a Windows computer or do I need to convert the code? Can you show how to convert it so it works the same on Windows or at least point me to an online document which shows how to convert it?

#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);
}

推荐答案

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

Borland C++ Builder encapsulated binary file access functions into:

FileOpen
FileCreate
FileRead
FileWrite
FileSeek
FileClose

以下是加载文本文件的简单示例:

Here simple example of loading text file:

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 包含检查或手动添加.

IIRC All these are part of VCL so in case you are using console you need to set VCL include check during the project creation or include it manually.

这篇关于转换Linux的打开,读取,写入,关闭功能以在Windows上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆