How to calc offset and read header binary?

I have binary image file gif format. How to simpless to take width & height my image? I try get it next, but unsuccesful.
1
2
3
4
5
6
					lseek(Data, 0x6, SEEK_SET); 
					read(Data, &Height, sizeof(Height)); 
					lseek(Data, 0x8, SEEK_SET); 
					read(Data, &Width, sizeof(Width)); 
					printf("Height: %d\r\n", Height); 
					printf("Width: %d\r\n", Width); 

Hex address 0x6 & 0x8 keeping value height & wight in according.
I don't understand the question. Are you getting absurd numbers? You might have an endianness problem.
And what's lseek, anyway?
If you haven't any clue what he is talking about then why post a reply?

The logical screen width is at offset 6, and the screen height is at offset 8.

You should be decoding it rather than relying on the machine size of any integer value:
1
2
3
4
5
6
7
8
  unsigned char buf[ 4 ];
  ...
  lseek( Data, 6, SEEK_SET );
  read( Data, buf, 4 );
  Width  = buf[ 0 ] +(buf[ 1 ] << 8);
  Height = buf[ 2 ] +(buf[ 3 ] << 8);
  printf( "Height: %d\r\n", Height );
  printf( "Width: %d\r\n", Width );

I'm not sure why you are putting that carriage return in the output though...

Hope this helps.
I don't understand him because he doesn't make himself clear, not because I don't know what the problem might be, smarty pants.
I was trying to obtain more information to provide a better answer.
"What's the simplest way of getting the height and width of an image"? Should I have said "getting it"?
If he had said "the real values are x and y but I'm getting a and b" it'd be a different story, but there are so many ways something can be "unsuccessful" he might as well not say anything.
He made himself perfectly clear: he stated what he is trying to do, the data he is trying to do it on, provided the exact code he is trying to it with, and the exact problem he is having with it. That's a perfect question by any standard.

If you are just going to acerbically attack everything that hits you the wrong way I'm going to start complaining to the site administrator.

Oh, and double-talk self-rationalizations doesn't impress me much.
For the love of God, are you serious?
So, to you, "unsuccessful" is an "exact problem"?

"Acerbically attack"? I'll use your logic on yourself. If there was any chance that I would react, why bother saying anything at all? Why not just answer his question and leave it at that? No. You needlessly made a remark about me. I might have overreacted (I hardly consider that overreacting, but what the hell), but that's my temperament. Who the **** are you to criticize it?

You know what? Go. Go complain to the admin, *******. I already know the Post Count Rule will apply, but what do I care? **** YOU.
Last edited on by admin
Please, stick to the topic.
helios, be cool and to follow smb.'s example from Duoas.
Duoas, thank for your answer.
I take next error to use your sample..
error: subscripted value is neither array nor pointer
on this row
Width = buf[ 0 ] + (buf[ 1 ] << 8);

A lot of about this...
I want take dec value from specify hex address into binary file.
If you don't declare buf as either an array (as I did on line 1) or as a pointer then you cannot use the [] operator (subscript operator) on it.

The only difference between decimal and hexadecimal is how it is written for us humans. 0xA and 10 are both the same number to the computer (and in reality).

Use scanf() to read a hex representation into an integer variable:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
  {
  unsigned int n;

  printf( "Please enter a number in hexadecimal> " );
  while (scanf( "%x", &n ) != 1)
    {
    printf( "What? Try again> " );
    scanf( "%*s\n" );
    }
  printf( "That number in decimal is %d\n", n );

  return 0;
  }

D:\prog\cc\foo> a
Please enter a number in hexadecimal> xyz
What? Try again> ./;
What? Try again> a
That number in decimal is 10

D:\prog\cc\foo>


Hope this helps.
thanks!
But, how to send buf[ 0 ] + (buf[ 1 ] << 8) to getchar()?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int hex() {
  unsigned int n;
  while ((n = getchar()) != 1)
    return (n);
    }
int main (int Wh){
char Data;
int *buf
int *Width;
...etc..
					read( Data, buf, 4 );
printf ("step2\n");
					*Width = buf[ 0 ] + (buf[ 1 ] << 8);
printf ("step3\n");
					Wh = hex(Width);
...etc...
}

valgrind
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
step1
==8868==
==8868== Syscall param read(buf) contains uninitialised byte(s)
==8868==    at 0x3D3E0C2A10: __read_nocancel (in /lib64/libc-2.6.so)
==8868==    by 0x4008D7: main (in /usr/src/src)
==8868== Warning: invalid file descriptor -16776672 in syscall read()
==8868==    at 0x3D3E0C2A10: __read_nocancel (in /lib64/libc-2.6.so)
==8868==    by 0x4008D7: main (in /usr/src/src)
step2
==8868==
==8868== Use of uninitialised value of size 8
==8868==    at 0x4008EA: main (in /usr/src/src)
==8868==
==8868== Use of uninitialised value of size 8
==8868==    at 0x4008F8: main (in /usr/src/src)
==8868==
==8868== Use of uninitialised value of size 8
==8868==    at 0x400909: main (in /usr/src/src)
==8868==
==8868== Process terminating with default action of signal 11 (SIGSEGV)
==8868==  Bad permissions for mapped region at address 0x4012A0
==8868==    at 0x400909: main (in /usr/src/src)
--8868-- REDIR: 0x3D3E073A90 (free) redirected to 0x4A05524 (free)
--8868-- REDIR: 0x3D3E078440 (memset) redirected to 0x4A06A20 (memset)
Last edited on
It looks to me like you never initialized Data, buf, nor Width.

You must open a file (and get its file descriptor) before you can read() it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
...

int Data;
...

Data = open( "fooey.gif", O_RDONLY );
if (Data < 0) 
  {
  fprintf( stderr, "Failure to open \"%s\"\n", "fooey.gif" );
  return 1;
  }
...


You have to set a pointer to point to something you are allowed to access before you can dereference it.
1
2
3
4
5
6
7
8
9
10
11
12
13
int *foo;

*foo = 42;      // <-- causes error
foo[ 3 ] = -7;  // <-- causes error

int x;
foo = &x;
*foo = 42;  // OK, because foo points to x

foo = malloc( sizeof( int ) *10 );
foo[ 3 ] = -7;  // OK, because foo points to dynamically-allocated memory
...
free( foo );


Was that it?
Last edited on
->You must open a file (and get its file descriptor) before you can read() it.
Yea, it step I be made.
Hm...
When I want to print Data, take undeclared (first use in this function)?
I've next listing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main (int Wh){
char Data;
int *buf
int *Width;
	File = fopen(/my_file,"r");
	if(File==NULL)perror("file size is zero\n");
	else{
	fread(Data,sizeof(char),128,File);
	             	    switch(n) {
				case one;
printf ("step\n", "Data:", bdData);
					lseek( Data, 6, SEEK_SET );
					read( Data, buf, 4 );
printf ("step1\n", "buf", buf);
printf ("step1,5\n", "Data:", Data);
					*Width = buf[ 0 ] + (buf[ 1 ] << 8);
printf ("step3\n");
					Wh = hex(Width); 
break; //case one
case two;

...etc...
break;
default:
	break;
}/switch

}//main 
Last edited on
It looks to me like you are a bit more confused than I first thought.

You absolutely must declare all variables before you use them, and they must be the correct type.

Also, you cannot (well, should not) mix FILE* functions with File Descriptor functions. Choose one or the other.
fopen() returns a FILE*.
lseek() and read() don't use FILE*s, they use file descriptors.

Use fopen() with fclose(), fseek(), and fread() (FILE*).
Use open() with close(), lseek(), and read() (file descriptor).

(A file descriptor is an int. If it helps, give it a type name with typedef.)
1
2
3
4
5
6
7
8
9
10
11
typedef int FileDescriptor;
...
char buf[ 4 ];
FileDescriptor f;
...
f = open( "fooey.gif", O_RDONLY );
if (f < 0) fooey();
lseek( f, 6, SEEK_SET );
read( f, buf, 4 );
...
close( f );

or
1
2
3
4
5
6
7
8
9
10
...
char buf[ 4 ];
FILE* f;
...
f = fopen( "fooey.gif", "rb" );
if (f == NULL) fooey();
fseek( f, 6, SEEK_SET );
fread( buf, 1, 4, f );
...
fclose( f );


Oh, one last thing. Main must look like one of the following:
int main() (C89) (C99)
int main( int argc, char* argv[] ) (C89) (C99)
int main( void ) (C99 only)

Fix those things, try to compile, and post your full code and errors and we'll see what we can do.
Strange, but it did work incorrectly. Value equals 10 in dec with any input a file.
1
2
3
4
5
6
7
8
9
10
11
12
int hex() {
  unsigned int n;
  while ((n = getchar()) != 1)
    return (n);
    }
main(){
int *H;
int He;
............next body a program.................
H=&He;
He  = buf[ 9 ] +(buf[ 10 ] << 8);
}

It possible take without pointers?
p.s. Anybody have share ebook C++ for Real Programmers?
Last edited on
I think you need to spend some time looking through some tutorials on handling pointers and arrays. I don't really see why you need to use pointers at all, unless you are trying to return values through function arguments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <string.h>
...
int get_GIF_dimensions( const char* filename, int* width, int* height )
  {
  FILE*         fp;
  unsigned char buf[ 13 ];  // room for Header and LSD

  fp = fopen( filename, "rb" );
  if (fp == NULL) return FALSE;

  // get Header and LSD and verify that it is a GIF file
  if ((fread( buf, 1, 13, fp ) != 13)
  ||  (strncmp( buf, "GIF", 3 ) != 0))
    {
    fclose( fp );
    return FALSE;
    }

  // store the logical screen width and height
  *width  = buf[ 6 ] +(buf[ 7 ] << 8);
  *height = buf[ 8 ] +(buf[ 9 ] << 8);

  fclose( fp );
  return TRUE;
  }

To use it:
1
2
3
4
5
int width, height;
if (!get_GIF_dimensions( "fooey.gif", &width, &height ))
  printf( "I could not open \"fooey.gif\"\n" );
else
  printf( "width, height = %u,%u\n", width, height );


People are always asking about books on the forum. I'm sure a search would return a lot of useful information.

Hope this helps.
Topic archived. No new replies allowed.