www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - return file from memory

reply Charma <Motoko_Kusanagi web.de> writes:
Once again i have a problem abusing this forum for help, sorry.

I am looking for a way to return a part of a File in a function without writing
it to hdd but sending the part itself as a File.
Maybe someone has an idea how to do that?

File myfunction(...requestedPart...)
{
  File bigfile = new File(bigfilename, FileMode.In);
  ...
  bigfile.read(request, requested part);
  ...
  return request;
}

Now i want this "request" not to be a string with the content but a File-Type
itself without saving it to hdd. I want it to be saved in memory only but be
able to handle same like File-type. Anything like that possible?
Maybe i need to make my own class from File or something?!?
Thanks for any help!
May 25 2007
next sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Charma wrote:
 Once again i have a problem abusing this forum for help, sorry.
Perhaps you should just start using digitalmars.D.learn, that's what it's there for ;)...
 I am looking for a way to return a part of a File in a function without
writing it to hdd but sending the part itself as a File.
 Maybe someone has an idea how to do that?
 
 File myfunction(...requestedPart...)
 {
   File bigfile = new File(bigfilename, FileMode.In);
   ...
   bigfile.read(request, requested part);
   ...
   return request;
 }
 
 Now i want this "request" not to be a string with the content but a File-Type
itself without saving it to hdd. I want it to be saved in memory only but be
able to handle same like File-type. Anything like that possible?
 Maybe i need to make my own class from File or something?!?
 Thanks for any help!
Any particular reason you want to return it as a File if it's not, you know, a _file_? Perhaps it'd be easier to just change the return type to Stream, and return a SliceStream? Or a MemoryStream if you insist on pre-reading the data, for example if you're paranoid about write operations changing the contents of the file while you're working with it.
May 25 2007
prev sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Charma wrote:
 Once again i have a problem abusing this forum for help, sorry.
You might consider posting these questions to digitalmars.d.learn instead. Then no one would have a reason to complain.
 I am looking for a way to return a part of a File in a function without
writing it to hdd but sending the part itself as a File.
 Maybe someone has an idea how to do that?
 
 File myfunction(...requestedPart...)
 {
   File bigfile = new File(bigfilename, FileMode.In);
   ...
   bigfile.read(request, requested part);
   ...
   return request;
 }
 
 Now i want this "request" not to be a string with the content but a File-Type
itself without saving it to hdd. I want it to be saved in memory only but be
able to handle same like File-type. Anything like that possible?
 Maybe i need to make my own class from File or something?!?
 Thanks for any help!
Probably you want to look at std.stream.MemoryStream. --bb
May 25 2007
parent reply Charma <Motoko_Kusanagi web.de> writes:
Bill Baxter Wrote:

 Charma wrote:
 Once again i have a problem abusing this forum for help, sorry.
You might consider posting these questions to digitalmars.d.learn instead. Then no one would have a reason to complain.
 I am looking for a way to return a part of a File in a function without
writing it to hdd but sending the part itself as a File.
 Maybe someone has an idea how to do that?
 
 File myfunction(...requestedPart...)
 {
   File bigfile = new File(bigfilename, FileMode.In);
   ...
   bigfile.read(request, requested part);
   ...
   return request;
 }
 
 Now i want this "request" not to be a string with the content but a File-Type
itself without saving it to hdd. I want it to be saved in memory only but be
able to handle same like File-type. Anything like that possible?
 Maybe i need to make my own class from File or something?!?
 Thanks for any help!
Probably you want to look at std.stream.MemoryStream. --bb
Thanks, i have tryed MemoryStream now but it looks like read(), readLine() and so on don't work anymore.. MemoryStream loadFile(char[] fname) { BufferedFile load = new BufferedFile(fname, FileMode.In); ... ...seek(..) ubyte[] buff; buff.length = sizeOfRequestedFile; ... load.read(buff); writefln(buff); // <-- up to here it seems to work perfectly ... MemoryStream Request = new MemoryStream; Request.reserve(sizeOfRequestedFile); Request.write(buff); // here i write it into the MemoryStream return Request; } this is where i use it: MemoryStream xy = bla.loadFile("text3.txt"); char[] asd; while(!xy.eof()) { xy.readLine(asd); writefln("xy: ", asd); } i get no output for some reason... the MemoryStream seems to be empty still... but no compiler-errors... What did i do wrong?
May 25 2007
parent reply Charma <Motoko_Kusanagi web.de> writes:
Charma Wrote:

 Bill Baxter Wrote:
 
 Charma wrote:
 Once again i have a problem abusing this forum for help, sorry.
You might consider posting these questions to digitalmars.d.learn instead. Then no one would have a reason to complain.
 I am looking for a way to return a part of a File in a function without
writing it to hdd but sending the part itself as a File.
 Maybe someone has an idea how to do that?
 
 File myfunction(...requestedPart...)
 {
   File bigfile = new File(bigfilename, FileMode.In);
   ...
   bigfile.read(request, requested part);
   ...
   return request;
 }
 
 Now i want this "request" not to be a string with the content but a File-Type
itself without saving it to hdd. I want it to be saved in memory only but be
able to handle same like File-type. Anything like that possible?
 Maybe i need to make my own class from File or something?!?
 Thanks for any help!
Probably you want to look at std.stream.MemoryStream. --bb
Thanks, i have tryed MemoryStream now but it looks like read(), readLine() and so on don't work anymore.. MemoryStream loadFile(char[] fname) { BufferedFile load = new BufferedFile(fname, FileMode.In); ... ...seek(..) ubyte[] buff; buff.length = sizeOfRequestedFile; ... load.read(buff); writefln(buff); // <-- up to here it seems to work perfectly ... MemoryStream Request = new MemoryStream; Request.reserve(sizeOfRequestedFile); Request.write(buff); // here i write it into the MemoryStream return Request; } this is where i use it: MemoryStream xy = bla.loadFile("text3.txt"); char[] asd; while(!xy.eof()) { xy.readLine(asd); writefln("xy: ", asd); } i get no output for some reason... the MemoryStream seems to be empty still... but no compiler-errors... What did i do wrong?
I'm very sorry... the stupid me forgot to seek to position 0 again before reading! Everything works perfect now! Thanks a lot!
May 25 2007
parent BCS <ao pathlink.com> writes:
Reply to Charma,

 writefln(buff); // <-- up to here it seems to work perfectly
 ...
 MemoryStream Request = new MemoryStream;
 Request.reserve(sizeOfRequestedFile);
 Request.write(buff); // here i write it into the MemoryStream
 
 return Request;
 }
[...]
 I'm very sorry... the stupid me forgot to seek to position 0 again
 before reading! Everything works perfect now! Thanks a lot!
 
MemoryStream Request = new MemoryStream(buff); // no seek(0) needed
May 25 2007