www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Operate with void[]

reply "Sergey" <httpal gmail.com> writes:
  Hello!

I have the following code:

//--------------------------------
import std.stdio;
import std.file;
import std.array;

int main(){
     void[] str = std.file.read("some_file.txt");
     writeln(str);
     // [32, 32, 55, 56, 9,10]

     // I'm looking for 10 and I change it to 9,9,9,10.
     // and I want to get this [32, 32, 55, 56, 9,9,9,9,10]

     std.file.write("some_file.txt", str2);
     return 0;
}
//--------------------------------

How do I operate with "str" to get the desired result?

Thanks in advance.
Regards, Sergey
Jan 14 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
You can't really use void[] directly - cast it to ubyte[] then 
use it and you can write each index by byte.
Jan 14 2014
parent reply "Sergey" <httpal gmail.com> writes:
On Wednesday, 15 January 2014 at 02:56:01 UTC, Adam D. Ruppe 
wrote:
 You can't really use void[] directly - cast it to ubyte[] then 
 use it and you can write each index by byte.
And then back to void[] write to a file?
Jan 14 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 15 January 2014 at 02:58:50 UTC, Sergey wrote:
 And then back to void[] write to a file?
That's done automatically - any array will implicitly cast to const(void)[]. If you're writing a function that can take any kind of data, for example, std.file.write, it is nice to take a const void[] since then you can pass anything to it: write("myfile", "a string"); // ok write("myfile", [0, 1, 2,3]); // ok and so on. Going from void[] to any other array requires a cast, since it needs to know what kind of data you want to look at (void has no meaning itself, so indexing it would be nonsense), but going from something else to void happens implicitly.
Jan 14 2014
parent reply "Sergey" <httpal gmail.com> writes:
On Wednesday, 15 January 2014 at 03:11:27 UTC, Adam D. Ruppe 
wrote:
 On Wednesday, 15 January 2014 at 02:58:50 UTC, Sergey wrote:
 And then back to void[] write to a file?
That's done automatically - any array will implicitly cast to const(void)[]. If you're writing a function that can take any kind of data, for example, std.file.write, it is nice to take a const void[] since then you can pass anything to it: write("myfile", "a string"); // ok write("myfile", [0, 1, 2,3]); // ok and so on. Going from void[] to any other array requires a cast, since it needs to know what kind of data you want to look at (void has no meaning itself, so indexing it would be nonsense), but going from something else to void happens implicitly.
So, please tell me how do I make replace: //------------- import std.stdio; import std.file; import std.array; int main(){ auto fl_txt = "E:/hosp/TMP/file.TXT"; void[] str = std.file.read(fl_txt); ubyte[] pert = cast(ubyte[])str; str2[0] = replace(pert, [9], [9,9,10]); // ????????? std.file.write(fl_txt, pert); return 0; } //-------------
Jan 14 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 15 January 2014 at 03:53:18 UTC, Sergey wrote:
 	str2[0] = replace(pert, [9], [9,9,10]); // ?????????	
Try changing that line to this: pert = replace(pert, cast(ubyte[]) [9], cast(ubyte[]) [9,9,10]); There's no str2 variable, so I assigned to pert instead. I casted the other arrays to ubyte[] because otherwise it throws all kinds of errors about ints instead of bytes (number literals are ints and the replace function is a bit picky)
Jan 14 2014
parent reply "Sergey" <httpal gmail.com> writes:
On Wednesday, 15 January 2014 at 04:04:05 UTC, Adam D. Ruppe 
wrote:
 On Wednesday, 15 January 2014 at 03:53:18 UTC, Sergey wrote:
 	str2[0] = replace(pert, [9], [9,9,10]); // ?????????	
Try changing that line to this: pert = replace(pert, cast(ubyte[]) [9], cast(ubyte[]) [9,9,10]); There's no str2 variable, so I assigned to pert instead. I casted the other arrays to ubyte[] because otherwise it throws all kinds of errors about ints instead of bytes (number literals are ints and the replace function is a bit picky)
Sorry, str2 is my mistake. And I think the last question: when the number of bytes with the number 9 is not known how to attach them? pert = replace(pert, cast(ubyte[]) [9], cast(ubyte[])[9] + ubyte[])[9] + ...n... + ubyte[])[10]);
Jan 14 2014
parent "Sergey" <httpal gmail.com> writes:
On Wednesday, 15 January 2014 at 05:21:26 UTC, Sergey wrote:
 On Wednesday, 15 January 2014 at 04:04:05 UTC, Adam D. Ruppe 
 wrote:
 On Wednesday, 15 January 2014 at 03:53:18 UTC, Sergey wrote:
 	str2[0] = replace(pert, [9], [9,9,10]); // ?????????	
Try changing that line to this: pert = replace(pert, cast(ubyte[]) [9], cast(ubyte[]) [9,9,10]); There's no str2 variable, so I assigned to pert instead. I casted the other arrays to ubyte[] because otherwise it throws all kinds of errors about ints instead of bytes (number literals are ints and the replace function is a bit picky)
Sorry, str2 is my mistake. And I think the last question: when the number of bytes with the number 9 is not known how to attach them? pert = replace(pert, cast(ubyte[]) [9], cast(ubyte[])[9] + ubyte[])[9] + ...n... + ubyte[])[10]);
again sorry....:))) some_ubyte = some_ubyte ~ cast(ubyte[])[9]
Jan 14 2014