Binary Output!!!

Hi, I have the following C code and I would like some help translating it to Delphi!!


int print_file(const char *arch, const char *content)
{
  FILE *f;
  int c;
  if ((f=fopen(arch,"rb")) == NULL) {
    printf ("File could not be opened: %s\n",arch);
    return 0;
  }
  setmode( fileno( stdout ), O_BINARY );
  while ((c = fgetc(f)) != EOF)
    putchar(c);
  fclose(f);
  return 1;
}

All this code does is to print out to the standard output the contents of a file.  The problem I have is with the function
  setmode( fileno( stdout ), O_BINARY );

This function converts the standard output (which is by default text) to binary.

Example:

if in DOS Prompt I type

C:\>type image.gif > image2.gif

It won't work (since the ">" operator doesn't specify a binary output).

Thank you very much,

Nadia :o)

BTW, this code compiles with Borland C++ (or C++ Builder).  I haven't tried it with VC, Wattcom or Symantec's C++
peimbertAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rwilson032697Connect With a Mentor Commented:
Ta!

Cheers,

Raymnod.
0
 
peimbertAuthor Commented:
Edited text of question.
0
 
rwilson032697Commented:
I would translate this code like this:

Function PrintFile(arch : TFileName) : Boolean;
var
  f : File of Byte;
  c : byte;
begin
  result := False;
  assignfile(f, arch);
  resetfile(f);
  if IOresult <> 0 then
    begin
      ShowMessage(format('Could not open %s', [arch]));
      exit;
    end;

  while not eof(f) do
    begin
      read(f, c);
      write(char(c));
    edn;
 
  CloseFile(f);
  Result := True;
end;

Cheers,

Raymond.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LischkeCommented:
Hi Nadia,

one should add that EOF in Delphi does not return True if a Ctrl+Z character appears (as fgetc in C) so you don't need to set the file input into binary mode. Consequently, there's no function in Delphi to do this. EOF is implemented by comparing the file size with its position thus the content doesn't matter.

Ciao, Mike
0
 
peimbertAuthor Commented:
Thank you very much Ray, Mike!!   I got it working!

Dam, that was so easy to try out :(

I once had to make a program that did this in C++, and if it wheren't because the setmode function, my app wouldn't of run.

Well ray, this was a total rip off ;-)))  ....why didn't I just print the file without worrying about the setmode function ????

Raymond, can you answer this question plz?


Thank you both,

Nadia
0
 
LischkeCommented:
Nadia, there's an "accept comment as answer" on each cyan colored line on the page. Click this link to accept a comment as answer.

Ciao, Mike
0
All Courses

From novice to tech pro — start learning today.