6

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);
}
alk
  • 69,737
  • 10
  • 105
  • 255
homebase
  • 713
  • 1
  • 6
  • 20
  • Yes, convert the code. Or better, write portable code at the beginning. Do it in anyway you want. / Note that questions asking for off-site resources are off-topic for StackOverflow. – user202729 Feb 27 '18 at 04:50
  • I'm pretty sure I've used these functions in Windows. Is it just a matter of including the proper header file? – Mark Ransom Feb 27 '18 at 04:53
  • 3
    There’s various unistd.h implementations for Windows, which is your only real problem. It’s basically just a few #includes and some #defines. – zzxyz Feb 27 '18 at 04:58

3 Answers3

6

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.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
4

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 2
    Microsoft has been very consistent in recent years of renaming functions that aren't part of the standard to have leading underscores. Presumably it's to prevent name collisions with user functions. Makes it kind of a pain to get Linux interoperability though. – Mark Ransom Feb 27 '18 at 05:39
  • I am glad they support the low level functions. They make it possible to write cross platform applications with a bit of `#ifdef/#else/#endif`. – R Sahu Feb 27 '18 at 05:45
  • *The corresponding functions in Windows use the same name but with an underscore (_) prepended to the name.* Not necessary. [Microsoft supports `open()`, for example](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/open), albeit with the misleading "deprecated" label Microsoft seems to apply to any function that could lead to writing portable code. – Andrew Henle Feb 27 '18 at 10:42
  • @AndrewHenle, thanks for that additional info. I have only ever used the _ variants of those functions. – R Sahu Feb 27 '18 at 16:47
1

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.

Spektre
  • 49,595
  • 11
  • 110
  • 380