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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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
rwilson032697Commented:
Ta!

Cheers,

Raymnod.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.