Solved

reading bytes from memory tables in ANSI C

Posted on 2004-04-16
8
225 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 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

6 Experts available now in Live!

Get 1:1 Help Now