Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert the Linux open, read, write, close functions to work on 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);
}
like image 275
homebase Avatar asked Feb 27 '18 04:02

homebase


People also ask

How does read () work in C?

The read() function reads data previously written to a file. If any portion of a regular file prior to the end-of-file has not been written, read() shall return bytes with value 0. For example, lseek() allows the file offset to be set beyond the end of existing data in the file.

What does open () do in C?

The open function creates and returns a new file descriptor for the file named by filename . Initially, the file position indicator for the file is at the beginning of the file.

What does the open () returns on success?

On success, open(), openat(), and creat() return the new file descriptor (a nonnegative integer). On error, -1 is returned and errno is set to indicate the error.

What is write function in C?

DESCRIPTION. The write() function shall attempt to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes.


3 Answers

Microsoft directly supports POSIX-style low-level IO calls such as open(), read(), , write(), and close(); although with what appears to be a misleading "deprecated" characterization.

The required header is <io.h>.

The calls correspond to functions named with a preceeding underscore, so 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

Some of the low-level functions may not have a non-underscore, POSIX-style equivalent name.

like image 101
Andrew Henle Avatar answered Oct 16 '22 22:10

Andrew Henle


The corresponding functions in Windows use the same name but with an underscore (_) prepended to the name.

open -> _open
close -> _close

etc.

They are declared in the header io.h. See https://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx for the list of all supported functions.

like image 30
R Sahu Avatar answered Oct 16 '22 22:10

R Sahu


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 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.

like image 24
Spektre Avatar answered Oct 16 '22 20:10

Spektre