Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

fopen problem

0 views
Skip to first unread message

Jeff Siegel

unread,
Jun 21, 1996, 3:00:00 AM6/21/96
to rec.games.programmer

Hello, I've been trying to get a function that loads a PCX file to work
properly. All of it compiles fine, but when I run the program with the
function, the thing messes up. Here's the code:

void PCX_Load(char *filename, pcx_picture_ptr image,int enable_palette)
{
// this function loads a pcx file into a picture structure, the actual image
// data for the pcx file is decompressed and expanded into a secondary buffer
// within the picture structure, the separate images can be grabbed from this
// buffer later. also the header and palette are loaded

FILE *fp;
int num_bytes,index;
long count;
unsigned char data;
char far *temp_buffer;

// open the file

if ((fp = fopen(filename,"rb")) == NULL) { <------- This is the problem
Set_Mode(TEXT_MODE);
printf("Could not load pcx file.");
exit(1);
}

// load the header

temp_buffer = (char far *)image;

for (index=0; index<128; index++)
{
temp_buffer[index] = getc(fp);
} // end for index

// load the data and decompress into buffer

count=0;

while(count<=SCREEN_WIDTH * SCREEN_HEIGHT)
{
// get the first piece of data

data = getc(fp);

// is this a rle?

if (data>=192)
{
// how many bytes in run?

num_bytes = data-192;

// get the actual data for the run

data = getc(fp);

// replicate data in buffer num_bytes times

while(num_bytes-->0)
{
image->buffer[count++] = data;

} // end while

} // end if rle
else
{
// actual data, just copy it into buffer at next location

image->buffer[count++] = data;

} // end else not rle

} // end while

// move to end of file then back up 768 bytes i.e. to begining of palette

fseek(fp,-768L,SEEK_END);

// load the pallete into the palette

for (index=0; index<256; index++)
{
// get the red component

image->palette[index].red = (getc(fp) >> 2);

// get the green component

image->palette[index].green = (getc(fp) >> 2);

// get the blue component

image->palette[index].blue = (getc(fp) >> 2);

} // end for index

fclose(fp);

// change the palette to newly loaded palette if commanded to do so

if (enable_palette)
{

for (index=0; index<256; index++)
{

Set_Palette_Register(index,(RGB_color_ptr)&image->palette[index]);

} // end for index

} // end if change palette

} // end PCX_Load

The code came from chapter five in the book Tricks of the Game Programming
Gurus. I'm using Watcom C/C++ 10.6 to compile this. The problem occures at
the line

if ((fp = fopen(filename,"rb")) == NULL) {

I tried a simple program to find out if fopen was messed up and it worked
fine. I would appreciate any help.

Jeff Siegel

unread,
Jun 21, 1996, 3:00:00 AM6/21/96
to

Maurice Fox

unread,
Jun 22, 1996, 3:00:00 AM6/22/96
to

On 6/21/96 12:54PM, in message <31CAD3...@freenet.edmonton.ab.ca>, Jeff
Siegel <je...@freenet.edmonton.ab.ca> wrote:

> Hello, I've been trying to get a function that loads a PCX file to work
> properly. All of it compiles fine, but when I run the program with the
> function, the thing messes up. Here's the code:
>
> void PCX_Load(char *filename, pcx_picture_ptr image,int enable_palette)
> {
> // this function loads a pcx file into a picture structure, the actual image
> // data for the pcx file is decompressed and expanded into a secondary buffer
> // within the picture structure, the separate images can be grabbed from this
> // buffer later. also the header and palette are loaded
>
> FILE *fp;
> int num_bytes,index;
> long count;
> unsigned char data;
> char far *temp_buffer;
>
> // open the file
>
> if ((fp = fopen(filename,"rb")) == NULL) { <------- This is the problem
> Set_Mode(TEXT_MODE);
> printf("Could not load pcx file.");
> exit(1);
> }
>
>

> The code came from chapter five in the book Tricks of the Game Programming
> Gurus. I'm using Watcom C/C++ 10.6 to compile this. The problem occures at
> the line
>
> if ((fp = fopen(filename,"rb")) == NULL) {
>
> I tried a simple program to find out if fopen was messed up and it worked
> fine. I would appreciate any help.

It would be useful to inspect the value of errno (print it out) if the fopen
fails and returns NULL. Look up the variable errno and the function perror in
your run-time library documentation. The reason associated with the value of
errno may solve your problem.

Lawrence Kirby

unread,
Jun 22, 1996, 3:00:00 AM6/22/96
to

In article <31CAD3...@freenet.edmonton.ab.ca>
je...@freenet.edmonton.ab.ca "Jeff Siegel" writes:

>if ((fp = fopen(filename,"rb")) == NULL) { <------- This is the problem

This looks fine. Unfortunately you didn't explain what actually goes wrong
and why you believe the problem is at this point.

...

>The code came from chapter five in the book Tricks of the Game Programming
>Gurus. I'm using Watcom C/C++ 10.6 to compile this. The problem occures at
>the line
>
>if ((fp = fopen(filename,"rb")) == NULL) {
>
>I tried a simple program to find out if fopen was messed up and it worked
>fine. I would appreciate any help.

So maybe it isn't going wrong here.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------

Lawrence Kirby

unread,
Jun 23, 1996, 3:00:00 AM6/23/96
to

In article <N.062296....@vie-va17-12.ix.netcom.com>
maur...@ix.netcom.com "Maurice Fox" writes:

>It would be useful to inspect the value of errno (print it out) if the fopen
>fails and returns NULL. Look up the variable errno and the function perror in
>your run-time library documentation. The reason associated with the value of
>errno may solve your problem.

But bear in mind that the language doesn't require fopen() to set errno
to a meaningful value (or even at all).

Maurice Fox

unread,
Jun 23, 1996, 3:00:00 AM6/23/96
to

On 6/23/96 8:20AM, in message <835532...@genesis.demon.co.uk>, Lawrence
Kirby <fr...@genesis.demon.co.uk> wrote:

True what you say. However, as you know, C exists in the context of a
"standard C library," which most commercial compilers support to some degree.
Most of the folks posting questions here do not distinguish between the
language and the library. As a case in point, fopen() is not part of the
language. It's just a function.


Dan Pop

unread,
Jun 23, 1996, 3:00:00 AM6/23/96
to

maur...@ix.netcom.com (Maurice Fox) writes:

>On 6/23/96 8:20AM, in message <835532...@genesis.demon.co.uk>, Lawrence
>Kirby <fr...@genesis.demon.co.uk> wrote:
>
>> In article <N.062296....@vie-va17-12.ix.netcom.com>
>> maur...@ix.netcom.com "Maurice Fox" writes:
>>
>> >It would be useful to inspect the value of errno (print it out) if the fopen
>> >fails and returns NULL. Look up the variable errno and the function perror
>> in
>> >your run-time library documentation. The reason associated with the value
>> of
>> >errno may solve your problem.
>>
>> But bear in mind that the language doesn't require fopen() to set errno
>> to a meaningful value (or even at all).
>>

>True what you say. However, as you know, C exists in the context of a
>"standard C library," which most commercial compilers support to some degree.
>Most of the folks posting questions here do not distinguish between the
>language and the library. As a case in point, fopen() is not part of the
>language. It's just a function.

Nonsense. The standard C library is part of the C language definition
(check chapter 7 of the ISO C standard) and fopen() is _not_ just a
function. It is a standard C library function, which gives it a
special status.

Lawrence's point is that the definition of fopen(), from the C standard,
doesn't require it to set errno in any circumstance, although it allows
it to do so.

Dan
--
Dan Pop
CERN, CN Division
Email: Dan...@cern.ch
Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland

0 new messages