Solved

reading bytes from memory tables in ANSI C

Posted on 2004-04-16
8
238 Views
Last Modified: 2010-04-17
Well I have this void * bufPtr pointing somewhere in the memory and putting there a record from a .dat file.
I allocate memory for bufPtr with this: bufPtr=malloc(sizeof(recSize));

Now I want to find a way and read specific amount of bytes from bufPtr. For example read 4 bytes beggining from byte 60...And that is my problem!

Please help! Urgent!


jonn
0
Comment
Question by:jonn_g
  • 3
  • 2
8 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10842701
Hi jonn_g,

((char *) bufPtr )  this will cast your void * to char *

((char *) bufPtr ) + 60  --- points to the desired location .... you can then read as may bytes as you want

for ( i=0; i<4; i++ )
    printf ( "%c", ((char *) bufPtr ) + 60 + i );

Sunnycoder
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10843009
Hi jonn_g,
> Well I have this void * bufPtr pointing somewhere in the memory and
> putting there a record from a .dat file.
> I allocate memory for bufPtr with this: bufPtr=malloc(sizeof(recSize));

As you defined your record as a struct (recSize), you should also define bufPtr as
recSize* bufPtr;

Then you can simply access a field as defined in your record. Be aware that C applies padding to entries bigger than one byte. Check if you need a
#pragma pack
or something similar (it's compiler-specific) if your record offsets don't match your structure definition.

Cheers,
Stefan
0
 

Author Comment

by:jonn_g
ID: 10843150
I forgot to mention that in my first post that's why I'm doing this comment:

What I mostly need is not to read these bytes from the memory and print the fields, but to compare the fields with some given constant values. And that is the most difficult part...
Should I put the fields into specific variables and compare them or what...

For exaple I have the 4 bytes from the for ( i=0; i<4; i++ )
                                                           printf ( "%2f", ((char *) bufPtr ) + 60 + i );
and they represent a float number.

How should I compare them with a value i.e 7.5 when the only thing I shall use is a void *valPtr pointer to the constant value 7.5
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 10843217
Now that you know how to get that address, cast it to float * and compare

(float *) (((char *) bufPtr ) + 60 )

points to the float you want
0
 

Author Comment

by:jonn_g
ID: 10849699
Well I'm not the best when it comes to ANSI C programming...but i thought this was for sure.
Let's say we have the code

#include <stdio.h>

int test()
{
      return 1;

}

void main()
{
      int t;
      t=test();
      printf("t=%d\n", &t);
}

I expected that the output at the screen would be "t=1"....It's not it's something like 1245052 and doen't seem somehow connected to the value the test() functin returns...
The problem is how can I save the value that a function returns into a variable...In this specific case how can I save somewhere the value "1".

Sorry for continuing on this topic but it's urgent!
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 250 total points
ID: 10856502
printf takes the variable as argument while you are passing the address of the variable ...

May be you confused it with scanf which takes address of a variable as argument

change printf("t=%d\n", &t); to printf("t=%d\n", t); and it should work fine
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now