Solved

reading bytes from memory tables in ANSI C

Posted on 2004-04-16
8
246 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 91
What is difference between Expression and Statement in terms of Programming Language (C#) 6 86
Modify a small python script 19 108
Help Required 2 32
A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

773 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