tags:

views:

97

answers:

2
+2  Q: 

VB to c++ or c#?

I am trying to translate this code but I don't understand how to use the GET / PUT part of the code in another language like c++ or c#.

This is the code :

Private Sub cmd_Click()
Dim i As Integer, a As Integer
a = 10
For i = 1 To a
    Dim file As String
    Open "txt" For Binary As #1
    file = Space(LOF(1))
    Get #1, , file
    Close #1
    Randomize
    Open "txtpath" & "\" & i & "txtname" For Binary As #1
    Put #1, , file
    Put #1, , Rnd
    Close #1
Next i
End Sub

The code could have bug because I replaced variables with plain text. What I understand is that the code get a file and then save it with some random data added to make it look different than the original. I don't use vb since years and I don't remember anything about it. Can someone help me porting this snippet to c++ or c#?

+1  A: 

Get and Put are simply used to read and write binary data from the file.

The program you posted basically does this 10 times for 10 different files.

system("copy txt txtpath\\1.txtname"); //just copy the file

//and then append some random junk
FILE *f = fopen("txtpath\\1.txtname", "a");
srand(time(NULL));
float rnd = (double)rand() / RAND_MAX;
fwrite(&rnd, sizeof(rnd), 1, f);
fclose(f);
Artelius
A: 

C#

private void cmd_Click()
{
   int i, a = 10;
   Random r = new Random();
   for(i = 1; i <= a; i++)
   {
       List<byte> file = new List<byte>();
       file.AddRange(System.IO.File.ReadAllBytes("txt"));
       file.AddRange(BitConverter.GetBytes((float)r.NextDouble()));
       System.IO.File.WriteAllBytes(String.Format(@"txtpath\{0}txtname", i), file.ToArray());
   }
}
volody