Not sure where to post this: equivalent of C's getc() function in Pascal? 
Author Message
 Not sure where to post this: equivalent of C's getc() function in Pascal?

I'm trying to convert a bit of C code into some Pascal code, and I ran
into trouble when trying to read data from a .PCX file.  In the pascal
code, I have the following defined:
FILENAME : file;
data:integer;

in C it looks like:
FILE *filename;
unsigned char data;

the function is called like this in C:
data = (unsigned char)getc(filename);

I tried doing the following in pascal:
BlockRead(FILENAME,data,1);
but I get some sort of file access problem or something.  Maybe my
pascal variables aren't declared right, or maybe BlockRead is the wrong
function.  could someone please help me out here?  Again, I didn't know
where to post this letter, so please, no flames.  Thanks.

                    Jeff W.



Wed, 18 Jun 1902 08:00:00 GMT  
 Not sure where to post this: equivalent of C's getc() function in Pascal?
: I tried doing the following in pascal:
: BlockRead(FILENAME,data,1);
: but I get some sort of file access problem or something.  Maybe my
: pascal variables aren't declared right, or maybe BlockRead is the wrong

(1) Did you open the file?
        filename = fopen("some name","rb");

(2) fread is probably a better translation of blockread than getc.
        fread(arrayaddress, elementsize, arraysize, filename);

        fread(&data,1,1,filename);

--

the Queen who straits, the Queen of strife;|          Cupertino, California
with gasp of death or gift of breath       | (xxx)xxx-xxxx            95015
she brings the choice of birth or knife.   |         I don't use no smileys



Wed, 18 Jun 1902 08:00:00 GMT  
 Not sure where to post this: equivalent of C's getc() function in Pascal?

(comp.lang.c removed from Newsgroups: header.)

Quote:

>I'm trying to convert a bit of C code into some pascal code, and I ran
>into trouble when trying to read data from a .PCX file.  In the pascal
>code, I have the following defined:
>FILENAME : file;
>data:integer;
>in C it looks like:
>FILE *filename;
>unsigned char data;
>the function is called like this in C:
>data = (unsigned char)getc(filename);
>I tried doing the following in pascal:
>BlockRead(FILENAME,data,1);
>but I get some sort of file access problem or something.  Maybe my
>pascal variables aren't declared right, or maybe BlockRead is the wrong
>function.  could someone please help me out here?  Again, I didn't know
>where to post this letter, so please, no flames.  Thanks.

Prototype of getc():

#include <stdio.h>
int getc(FILE *stream);

Reading character by character works, but isn't exactly efficient.  You may
want to consider recoding the source with BlockRead() to read into a larger
buffer.

Anyway, here are some equivalents to your statments above:

var
  data : char;
  filename : file of char;

begin
  assign (filename,'my-image.pcx');
  reset (f);
  {...}
  read (f,data);
  {...}
  close (f);
end;

But if you write an image loader in this method, don't expect it to be very
fast.

Quote:
>                    Jeff W.

--
Scott F. Earnest           | We now return you to our regularly scheduled



Wed, 18 Jun 1902 08:00:00 GMT  
 Not sure where to post this: equivalent of C's getc() function in Pascal?

First: I think this is the right group, it's for Pascal right? :)

: I'm trying to convert a bit of C code into some pascal code, and I ran
: into trouble when trying to read data from a .PCX file.  In the pascal
: code, I have the following defined:
: FILENAME : file;
: data:integer;

: in C it looks like:
: FILE *filename;
: unsigned char data;

: the function is called like this in C:
: data = (unsigned char)getc(filename);

: I tried doing the following in pascal:
: BlockRead(FILENAME,data,1);
: but I get some sort of file access problem or something.  Maybe my
: pascal variables aren't declared right, or maybe BlockRead is the wrong
: function.  could someone please help me out here?

Second: It always helps if you incorporate some code (more then you have
done :) ). But I give it a try anyway.

Since you have fileaccess problems (*exactly* what errorcode did you get?)
a problem can be that you don't use assign()/reset() or the file is already
in use by another program. (I hope you you use close(), otherwise you are
going to have to many files open and MessDoze pukes)

Otherwise I can't see where it would go wrong... :-/

One less good thing is that you are using BlockRead(FILENAME,data,1) to
read one record. It's better to use  read(filename, data) for that.

Can you send us some of your Pascalcode? It would be easier to find the
problem (I hope... ;) ).

/Jonas
+------------------------------------------------------------------------+
| Who? Me?: Jonas Steverud                         | If All is One,      |

| Home....: http://www.dtek.chalmers.se/%7Ed4jonas |                     |
+------------------------------------------------------------------------+



Wed, 18 Jun 1902 08:00:00 GMT  
 Not sure where to post this: equivalent of C's getc() function in Pascal?

Quote:
>Scott F. Earnest           | We now return you to our regularly
scheduled


thanks for your help.

                    Jeff W.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Looking for equivalent to 'friend'(C++) in object pascal

2. Pascal equivalent of 2 QBasic-functions.

3. Not sure whats wrong with this (customer file listing)

4. Im sure its not my code, but......

5. why does post not always post?

6. Post not posting - D3

7. HELP -- Posting and not posting

8. Need Gnu equivalent for type 'Registers'

9. Equivalent of C 'Union'

10. Does Delphi have an equivalent to the Mid$ function in VB

11. equivalent MKSMBF$ function, Possible?

12. Equivalent to DisableDefault in Paradox's ObjectPAL?

 

 
Powered by phpBB® Forum Software