Вопрос

I got an assignment at my internship, which basically goes like this (Everything is to be made with C#, in Visual Studio 2013):

  1. Create new binary file (specific folder)
  2. Empty/delete file, if it already exists
  3. Mark start-time (millisecond precision)
  4. Write 10 GB of dummy data into file, as fast as possible. (Write data as chunks of 1024 byte dummy data, continue untill 10 GB has been written)
  5. Mark end-time (millisecond precision)
  6. Flush and close file
  7. Output the time it took to write all 10 GB of data (end time - start time)
  8. Output number of bytes per second.

Possible input parameters must be:

  • Folder
  • Total bytes (ex. 10GB)
  • Bytes per chunk (ex. 1024)

Now I am not really interested in just getting handed a code that does this, as that teaches me nothing. I am interested in learning how this is done, understand it, so I can do it myself.

Please also note, that I understand this is not a site to go to, just to have somebody make your assignments - but also understand that I am very interested in learning this, and I do not know of anywhere else (besides my tutor, but I don't want to come asking him about stuff every 20 minutes :P)

Could somebody explain, or maybe redirect me somewhere? Maybe just give me a hint as of how its done? I have been doing C# for ~2 weeks, but I have never written anything that creates files etc.

Это было полезно?

Решение

Create new binary file (specific folder)

...

Flush and close file

C# uses a concept called streams for binary input/output.

Mandatory literature: http://msdn.microsoft.com/en-us/library/k3352a4t(v=vs.110).aspx

Both techniques are demonstrated on MSDN.

Empty/delete file, if it already exists

Well, actually, you only have to check whether a file exists if you don't wish to overwrite it.

Mark start-time (millisecond precision)

...

Mark end-time (millisecond precision)

...

Output the time it took to write all 10 GB of data (end time - start time)

Output number of bytes per second.

Look around the System.Diagnostics namespace.

Write 10 GB of dummy data into file, as fast as possible. (Write data as chunks of 1024 byte dummy data, continue untill 10 GB has been written)

Finally, the meat of the program...


Spoiler alert!


You'll have to write a loop that writes 1024 bytes on every iteration until the total sum of bytes written reaches 10GB.

Below is a code example, but without error checking (since it's not a requirement). In the real world, you would obviously write code that handles exceptions too.

using (var fileStream = File.OpenWrite(path))
{
    double fileSize = Math.Pow(10, 10); // 10 GB
    int bufferSize = 1024; // 1KB
    byte[] buffer = new byte[bufferSize]; // Create an array of zero's

    for (int written = 0; written < fileSize; written += bufferSize)
    {
        // Write 1024 bytes (all zero's: 000000000000...0)
        fileStream.Write(
            array: buffer,
            offset: 0,
            count: bufferSize);
    }
    /* Optional inside a "using" block: */ fileStream.Flush();
}

Другие советы

Have a look at System.IO.File. You want to create a file stream (the static File.Create), and then in a using clause, you're going to write data through that stream using Stream.Write. I'm not going to add more, you can find enough information on MSDN etc. But feel free to ask any questions if need be.

Also, to nitpick, Mark start-time (millisecond precision) is going to be quite tricky, since system date isn't going to guarantee you that kind of precision. So you can print out the milliseconds, but it's not going to be accurate to a millisecond. There's a specialized class for measuring time intervals, find it and use it :)

Check out the following to get you started:

  • System.IO.FileStream
  • System.IO.FileMode
  • System.Random
  • System.Diagnostics.Stopwatch
  • System.TimeSpan
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top