How to generate a string of a certain length to be inserted into a file to meet file size criteria?

I have a requirement to check some download problems regarding file size. I have a Windows application written in C # that will automatically generate files. I know the size of each file, for example. 100 KB, and how many files to generate. I need help on how to create a line that is less than or equal to the required file size.

pseudo code:

long fileSizeInKB = (1024 * 100); //100KB int numberOfFiles = 5; for(var i = 0; i < numberOfFiles - 1; i++) { var dataSize = fileSizeInKB; var buffer = new byte[dataSize]; using (var fs = new FileStream(File, FileMode.Create, FileAccess.Write)) { } } 
+64
string c #
Feb 17 2018-11-17T00:
source share
4 answers

You can always use the constructor for a string that takes a char and several times when you want this character to be repeated:

 string myString = new string('*', 5000); 

This gives you a string of 5,000 stars - tailor it to your needs.

+189
Feb 17 2018-11-17T00:
source share

The easiest way is the following code:

 var content = new string('A', fileSizeInKB); 

You now have a row with as many A as required.

To populate it with Lorem Ipsum or some other duplicate line, create something like the following pseudocode:

 string contentString = "Lorem Ipsum..."; for (int i = 0; i < fileSizeInKB / contentString.Length; i++) //write contentString to file if (fileSizeInKB % contentString.Length > 0) // write remaining substring of contentString to file 

Edit: if you save in Unicode, you may need half the number of files, because unicode uses two bytes per character, if I remember correctly.

+10
Feb 17 2018-11-17T00:
source share

There are so many options how you can do this. One of them, fill the file with a bunch of characters. Do you need 100k? No problem .. 100 * 1024 * 8 = 819200 bits. The only char is 16 bits. 819200/16 = 51200. You need to insert 51,200 characters into the file. But note that the file may have additional header / metadata data, so you may need to consider this and reduce the number of characters to write to the file.

0
Feb 17 '11 at 17:13
source share

As a partial answer to your question, I recently created a portable WPF application that easily creates junk files of almost any size: https://github.com/webmooch/FileCreator

0
May 6 '14 at 6:20
source share



All Articles