0

I'm trying to read a large file from a disk and report percentage while it's loading. The problem is FileInfo.Length is reporting different size than my Encoding.ASCII.GetBytes().Length.

    public void loadList()
    {
        string ListPath = InnerConfig.dataDirectory + core.operation[operationID].Operation.Trim() + "/List.txt";
        FileInfo f = new FileInfo(ListPath);

        int bytesLoaded = 0;

        using (FileStream fs = File.Open(ListPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (BufferedStream bs = new BufferedStream(fs))
        using (StreamReader sr = new StreamReader(bs))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                byte[] array = Encoding.ASCII.GetBytes(line);
                bytesLoaded += array.Length;
            }
        }

        MessageBox.Show(bytesLoaded + "/" + f.Length);
    }

The result is

    13357/15251

There's 1900 bytes 'missing'. The file contains list of short strings. Any tips why it's reporting different file sizes? does it has to do anything with '\r' and '\n' characters in the file? In addition, I have the following line:

    int bytesLoaded = 0;

if the file is lets say 1GB large, do I have to use 'long' instead? Thank you for your time!

4

3 に答える 3

5
于 2013-06-07T09:58:53.633 に答える
1

これは、ReadLine によって飲み込まれる行末であり、ソース ファイルが ASCII よりも詳細なエンコーディング (おそらく UTF8 ですか?) であることが原因である可能性もあります。

int.MaxValueは 2147483647 なので、ファイルが 2GB を超える場合にintforを使用すると問題が発生します。bytesLoadedに切り替えますlong。結局のところ、FileInfo.Lengthとして定義されていlongます。

于 2013-06-07T10:00:12.293 に答える
0

The ReadLine method removes the trailing line termination character.

于 2013-06-07T09:59:06.267 に答える