verdana verdana - 1 month ago 8
C# Question

Generate 1 MB (or n MB) text file in C#

I need to create a text file (alphanumeric) with size (exact or nearly) in MB equal to my input number, such as 1 MB. I tried to generated a string, with think that one char is 16 bit or 2 byte, so:
1KB = 1024 Bytes = 1024 x 8 Bits = 1024 x 8 / 16 chars = 512 chars.
But the generated file is not very seem to be right :(
If I generate byte array and write it to file, the size is pretty correct!

public static void generateDummyFileBySize(long sizeInMb, string filePath) {

byte[] data = new byte[sizeInMb * 1024 * 1024];
Random rng = new Random();
rng.NextBytes(data);
File.WriteAllBytes(filePath, data);
}


Help me please. This is the code I used to generated text file

public static String generateStringSize(long sizeByte)
{

StringBuilder sb = new StringBuilder();
Random rd = new Random();
/**
* Byte -> Bit -> Divide by 16 to num of char
* */
var numOfChars = sizeByte * 8 ;
string allows = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int maxIndex = allows.Length - 1;
for (int i = 0; i < numOfChars; i++)
{
int index = rd.Next(maxIndex);
char c = allows[index];
sb.Append(c);
}
return sb.ToString();
}

public static void generateTextFileBySize(long size, string fileName)
{
long sizeOld = size;
try
{

String s;
String path = Directory.GetCurrentDirectory();
var physicPath = path + Path.DirectorySeparatorChar + fileName;
File.Delete(physicPath);
if (size <= MAX_SIZE)
{
s = StringUtil.generateStringSize(size);
Console.WriteLine("Generated a string with length " + size);
File.WriteAllText(physicPath, s, Encoding.UTF8);
}
else
{
while (size > MAX_SIZE)
{
s = StringUtil.generateStringSize(MAX_SIZE);
Console.WriteLine("Appending " + MAX_SIZE + " to file");
File.AppendAllText(physicPath, s, Encoding.UTF8);
size -= MAX_SIZE;
}
s = StringUtil.generateStringSize(size);
File.AppendAllText(physicPath, s, Encoding.UTF8);
}
Console.WriteLine("Created file named " + fileName + " with size " + sizeOld + " bytes.");
}
catch (Exception ex)
{
Console.WriteLine("Error when generating file");
Console.WriteLine(ex.Message);
}
}

Answer

The problem lies here:

var numOfChars = sizeByte * 8 ;

You create eight times the characters you need. Instead just use

var numOfChars = sizeByte;

Each of the characters you use occupies one byte when stored as UTF-8, so you don't need to use a different number of characters than the number of bytes you want.

Comments