473,378 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Reading memory-adresses from files

Hello NG,

is fscanf capable of reading a memory adress from a file? I tried to
find the right format-parameter, but my code doesn't seem to work...

Regards,
Sebastian

----cut-----
int i=120;
int *a;
FILE *Outfile;
if ((Outfile = fopen("tst.dat","w"))== NULL) //open
{
if ((Outfile = fopen("tst.dat","w+"))==NULL) //create
{
printf("Error opening file\n");
exit();
}
}
printf("%p\n",&i);

fprintf(Outfile,"%p\n",&i);
fclose(Outfile);
Outfile = fopen("tst.dat","r");

fscanf(Outfile,"%p",a);
fclose(Outfile);
printf("%p",a);
----cut-------
Jul 22 '05 #1
4 2076
Sebastian Becker wrote:
is fscanf capable of reading a memory adress from a file? I tried to
find the right format-parameter, but my code doesn't seem to work...


Any number can mean whatever you make it mean. For example, something
like 0x10000000 on Windows may mean the address from which a DLL is
loaded. It may actually be a valid address once you read that number
from a file and convert into an address.

When a program runs, objects in it are located at some addresses that
can be converted to numbers (long is usually large enough to represent
any object pointer value). While the program is running you can write
those values to a file and later (while the program is still running)
retrieve them from that file and use after converting back to pointers.
It is possible that those addresses will stay valid during the run of
the program, and you can actually know that yourself: pointers' lives
are well defined.

When the program exits, finishes, the memory it used to occupy and use
for the objects, is usually freed. Nothing can guarantee that the next
run of the same program will mean that the same objects are located at
the same memory addresses, generally.

So, the answer to your question is probably: while it is possible for
your program to store and retrieve addresses in/from files, there is no
much sense in doing that.

V
Jul 22 '05 #2
> It may actually be a valid address once you read that number
from a file and convert into an address.
This is exactly what I want to do, but my code doesn't work.

I want use a preemptiv multitasking "core" [1] to run 3-4 programs at
the same time. For IPC purposes there is a shared memory needed. Now, my
idea was to initialize variables in the first program (which will be
running the whole time) and hand over the adresses to the other
programs. Doing this the CPU can remain in real-mode.
So, the answer to your question is probably: while it is possible for
your program to store and retrieve addresses in/from files, there is no
much sense in doing that.


So, how do you program this retrival of pointers from files?

Thanks, Sebastian
[1] http://www.shamrock.de/dostools.htm#multitasking - unfortunately
only in german

mt.com [Parameters: filenames of the tasks (exe)]
- runs in real-mode
- preemptive multi-tasking
- tasks are switched by a reprogrammed keyboard interrupt
Jul 22 '05 #3
"Sebastian Becker" <sb*******@gmx.net> wrote...
It may actually be a valid address once you read that number
from a file and convert into an address.


This is exactly what I want to do, but my code doesn't work.

I want use a preemptiv multitasking "core" [1] to run 3-4 programs at the
same time. For IPC purposes there is a shared memory needed. Now, my idea
was to initialize variables in the first program (which will be running
the whole time) and hand over the adresses to the other programs. Doing
this the CPU can remain in real-mode.
So, the answer to your question is probably: while it is possible for
your program to store and retrieve addresses in/from files, there is no
much sense in doing that.


So, how do you program this retrival of pointers from files?


Just like I said, convert them to integers when printing out and then
read them as integers and convert from integers to pointers after they
have been read.

V
Jul 22 '05 #4
> Just like I said, convert them to integers when printing out and then
read them as integers and convert from integers to pointers after they
have been read.


Thanks, works fine now.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: guillaume | last post by:
I have to read and process a large ASCII file containing a mesh : a list of points and triangles. The file is 100 MBytes. I first tried to do it in memory but I think I am running out of memory...
6
by: Kevin T. Ryan | last post by:
Hi All - I'm not sure, but I'm wondering if this is a bug, or maybe (more likely) I'm misunderstanding something...see below: >>> f = open('testfile', 'w') >>> f.write('kevin\n') >>>...
4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
4
by: Yodai | last post by:
Hi all.. I'm trying to program an application for an emmbedded sistem where an external processor introduces data into my RAM. The thing is some if the data I have to pick up is written in...
7
by: eriwik | last post by:
Hi, I'm working on a small application which processes PNG-images and need to read parts of them into structures and/or variables and I was wondering what assumptions one can make about how the...
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
6
by: Gerald Maher | last post by:
Hi Reading in XML file using C#, changing elements and saving back to the file, how can i do that ? I want to be able to read an XML file and read out Elements. for example <name>Tom</tom> ...
0
by: Einar Høst | last post by:
Hi, I'm reading data from a text file, and having read a bit on performance and memory, I'm wondering how it could be done in the fastest, most frugal way. The data comes in messages, one per...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
19
by: Hapa | last post by:
Does only reading (never writing) of a variable need thread synchronisation? Thanks for help? PS. Anybody knows a Visual C++ news group?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.