reading from a file without actually writing into its hard disk copy

J

Jeevan

Hi,

I have an array of data (which I am getting from a socket connection).
I am working on a program which acts on this data but the program is
written to work on data from a file (not from an array). I cannot
change anything in the program but can add some features by which I
can convert this array of data into a file. The easiest thing would be
to write the data into a file (in hard disk) and use it. But I will be
working on thousands of such data, so this would be time consuming.

One more constraint is that, the data has to be written into a file in
binary format and read in ascii format (otherwise the data is not read
properly). Is there anyway I can achieve this (writing in binary
format and reading in ascii format) without actually writing the array
into harddisk (by just using RAM).

Thanks
Jeevan.
 
M

Malcolm

Jeevan said:
The easiest thing would be to write the data into a file (in hard disk)
and use it. But I will be working on thousands of such data, so this
would be time consuming.
tmpfile() is your friend. This is almost always implemented as a RAM file.
One more constraint is that, the data has to be written into a file in
binary format and read in ascii format (otherwise the data is not read
properly). Is there anyway I can achieve this (writing in binary
format and reading in ascii format) without actually writing the array
into harddisk (by just using RAM).
Unfortunately now you're in trouble, since you can't open a temporary file
as binary for writing and text for reading. The real solution is to change
your program design so that this isn't necessary.
 
S

Scott Xiao

Well, if you're in Unix/Linux, maybe you can use function "mmap".
The system call mmap() establishes a mapping between your process's
address space and a file.
You could map your file into RAM, then place your array on that memory
address.
But, maybe your data should be formated as ascii .....

Scott
 
J

Jeevan

Thanks for the suggestion. Someone suggested the following method. I
am collecting the data into the file created by tmpfile() (I am using
Windows so mmap does not work like that in unix). tmpfile() is created
in binary mode. Now to read the file in ascii mode, I have to change
the mode of the file. This is possible using the function _setmode()
in vc++. I dont think this is available in unix.

The reason why I am having to do this is that, webserver sends the
data as it is in bytes. So, if the data contains new line character
written by another c program in windows, it is actually the characters
'\r' '\n'. If I write this in ascii mode, then the file contains \r \r
\n (\n is replaced by \r \n). So I have to write it in binary mode. I
have to read it in ascii mode because there are 2 characters for new
line character.

One more solution to avoid this problem is to change the data sent by
webserver on the fly (array manipulations) which might take some extra
time.

Jeevan
 
G

Glen Herrmannsfeldt

(snip)
The reason why I am having to do this is that, webserver sends the
data as it is in bytes. So, if the data contains new line character
written by another c program in windows, it is actually the characters
'\r' '\n'. If I write this in ascii mode, then the file contains \r \r
\n (\n is replaced by \r \n). So I have to write it in binary mode. I
have to read it in ascii mode because there are 2 characters for new
line character.

One more solution to avoid this problem is to change the data sent by
webserver on the fly (array manipulations) which might take some extra
time.

It might take more time, but it is exactly what the system I/O routines will
do when reading ASCII mode files.

-- glen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top