none
Content of a newly created disk file RRS feed

  • Question

  • Suppose something like the following snippet:

    string filePath = @"C:\MyFolder\MyFile.dat";
    var fs = File.Create(filePath);
    fs.SetLength(1000000000);
    fs.Close();
    
    Is a newly created file always guaranteed to contain nulls only?

    Sunday, January 12, 2020 11:54 AM

All replies

  • Hi EuroEager,

    Thank you for posting here.

    I make a test based on your code.

    After I read the content in the file, I get "\0" which is not null.

    Code:

                string filePath = @"D:\TestFile2\MyFile.dat";
                var fs = File.Create(filePath);
                fs.SetLength(1000000000);
                fs.Close();
    
                const int bufferSize = 1024;
    
                var sb = new StringBuilder();
                var buffer = new Char[bufferSize];
                var length = 0L;
                var totalRead = 0L;
                var count = bufferSize;
    
                using (var sr = new StreamReader(filePath))
                {
                    length = sr.BaseStream.Length;
                    while (count > 0)
                    {
                        count = sr.Read(buffer, 0, bufferSize);
                        sb.Append(buffer, 0, count);
                        totalRead += count;
                        var s = sb.ToString();
                    }
                }

    Result:


    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 13, 2020 8:26 AM
    Moderator
  • I think I was unclear in my question, what I meant to ask was if the file is guaranteed to contain only null-bytes as if the following is added to he snippet and then always will report "Only zero-bytes was found"

    int byteValue;
    long zeroBytePosition = -1;
    using (fs = File.OpenRead(filePath))
    {
        while ((byteValue = fs.ReadByte()) != -1)
        {
            if (byteValue != 0)
            {
                zeroBytePosition = fs.Position;
                break;
            }
        }
        if (zeroBytePosition == -1) Console.WriteLine($"Only zero-bytes was found");
        else Console.WriteLine($"Non-zero byte found at stream position {zeroBytePosition}");
    }
    

    Monday, January 13, 2020 9:52 AM
  • Thanks for posting this!
    Monday, January 13, 2020 9:53 AM
  • According to documentation for SetLength and other functions, the new contents is undefined.

     

    In addition, I think that the contents will never contain data of previous deleted files from the same disk areas, for intimacy reasons.


    • Edited by Viorel_MVP Monday, January 13, 2020 8:25 PM
    Monday, January 13, 2020 11:19 AM
  • Ah, didn't see that and of course we expand the file (from default zero size).
    As you pinpoint, other functions like SetEndOfFile in kernel32.dll states the same.

    Does what you say in second paragraph mean that the content could be random, but definitely not "old file"? (The bytes of the stream must be something).

    Finally does this mean that a newly created file should always be initiated (written to) with something before it i allowed to be read, in other words trusting the unwritten content to be zero-bytes is a no-no?

    (B.t.w. and another matter: this post shows smaller fonts in parts of the first 2 lines, do you know how to change, I always struggle with this forum)

    Monday, January 13, 2020 11:47 AM
  • Hi EuroEager,

    According to the code in my reply above, all the content I read in the file is "\0", and you can write something to the file normally.

    Check the following references:

    1. Help with \0 terminated strings in C#
    2. In C#, what is the difference between "\0", '\0' and null?

    Hope them could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 14, 2020 2:15 AM
    Moderator
  • In my opinion, undefined contents, mentioned in documentation, means that it could be any value which is chosen by Operating System to be written explicitly programmatically (usually zero), or it could be any value (or random contents) if certain hardware or virtual drives are able (or will be able) to initialise the storage independently, or it could be the existing contents of old files in specific isolated environment when this does not represent an issue. It means that the experiments are not necessarily reproducible.

    • Edited by Viorel_MVP Tuesday, January 14, 2020 5:53 AM
    Tuesday, January 14, 2020 5:51 AM