Link to home
Start Free TrialLog in
Avatar of peimbert
peimbert

asked on

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++
Avatar of peimbert
peimbert

ASKER

Edited text of question.
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.
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of rwilson032697
rwilson032697

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial