C# ReadFile _ 方法返回切片为锯齿状数组的字符串

发布于 2025-01-09 14:17:02 字数 2201 浏览 0 评论 0原文

我需要根据条件实现一个方法:

char[][] Foo(StreamReader stream, int arraySize) {}

方法应该返回一个基础字符串,该字符串根据 arraySize 切成锯齿状的字符数组。

第一个条件。如果流是 string.Empty 并且 arraySize 是 10:

return Array.Empty<char[]>();

第二个条件。如果流是“Lorem”并且 arraySize 是 5:

return new char[][]
{
    new char[] { 'L', 'o', 'r', 'e', 'm' },
};

第三个条件。如果流是“Lorem”并且 arraySize 是 1:

return new char[][]
{
    new char[] { 'L' },
    new char[] { 'o' },
    new char[] { 'r' },
    new char[] { 'e' },
    new char[] { 'm' },
};

第四个条件。如果流是“Loremipsumdolorsitamet”并且 arraySize 是 5:

return new char[][]
{
    new char[] { 'L', 'o', 'r', 'e', 'm' },
    new char[] { 'i', 'p', 's', 'u', 'm' },
    new char[] { 'd', 'o', 'l', 'o', 'r' },
    new char[] { 's', 'i', 't', 'a', 'm' },
    new char[] { 'e', 't' },
};

使用我的解决方案不起作用 第四个条件;为了测试我的函数,我在 Program.cs 旁边创建了 temp.txt 文件,并将上面提到的值(Lorem、Loremipsumdolorsitamet)放在那里。

实现:

char[][] Foo(StreamReader stream, int arraySize) 
{
   long arrLength = 0;

    if (streamReader.BaseStream.Length % arraySize == 0)
    {
        arrLength = streamReader.BaseStream.Length / arraySize;
    }
    else
    {
        arrLength = streamReader.BaseStream.Length / arraySize + 1;
    }

    char[][] array = new char[arrLength][];

    for (int i = 0; i < array.Length; i++)
    {
        array[i] = new char[arraySize];

        for (int j = 0; j < arraySize; j++)
        {
            array[i][j] = (char)streamReader.Read();
        }
    }

    return array;
}

调用函数并测试结果:

int num = 5;

var myArray = Foo(new StreamReader(@"D:\temp.txt"), num);

for (int i = 0; i < myArray.Length; i++)
{
  for (int k = 0; k < myArray[i].Length; k++)
  {
    Console.Write(myArray[i][k] + "\t");
  }
  Console.WriteLine();
}

结果是:

L       o       r       e       m
i       p       s       u       m
d       o       l       o       r
s       i       t       a       m
e       t       ▒       ▒       ▒

那么,最后一个数组长度是 5(但应该是 - 2),符号是什么: '▒' ?

请帮忙!

I need to implement a method according to conditions:

char[][] Foo(StreamReader stream, int arraySize) {}

Method should return an underlying string that sliced into jagged array of characters according to arraySize.

First condition. If stream is string.Empty and arraySize is 10:

return Array.Empty<char[]>();

Second condition. If stream is "Lorem" and arraySize is 5:

return new char[][]
{
    new char[] { 'L', 'o', 'r', 'e', 'm' },
};

Third condition. If stream is "Lorem" and arraySize is 1:

return new char[][]
{
    new char[] { 'L' },
    new char[] { 'o' },
    new char[] { 'r' },
    new char[] { 'e' },
    new char[] { 'm' },
};

Forth condition. If stream is "Loremipsumdolorsitamet" and arraySize is 5:

return new char[][]
{
    new char[] { 'L', 'o', 'r', 'e', 'm' },
    new char[] { 'i', 'p', 's', 'u', 'm' },
    new char[] { 'd', 'o', 'l', 'o', 'r' },
    new char[] { 's', 'i', 't', 'a', 'm' },
    new char[] { 'e', 't' },
};

With my solution doesn't works Forth condition; To test my function I've created temp.txt file alongside Program.cs and put there above mentiond values (Lorem, Loremipsumdolorsitamet).

Implementation:

char[][] Foo(StreamReader stream, int arraySize) 
{
   long arrLength = 0;

    if (streamReader.BaseStream.Length % arraySize == 0)
    {
        arrLength = streamReader.BaseStream.Length / arraySize;
    }
    else
    {
        arrLength = streamReader.BaseStream.Length / arraySize + 1;
    }

    char[][] array = new char[arrLength][];

    for (int i = 0; i < array.Length; i++)
    {
        array[i] = new char[arraySize];

        for (int j = 0; j < arraySize; j++)
        {
            array[i][j] = (char)streamReader.Read();
        }
    }

    return array;
}

call function and test results:

int num = 5;

var myArray = Foo(new StreamReader(@"D:\temp.txt"), num);

for (int i = 0; i < myArray.Length; i++)
{
  for (int k = 0; k < myArray[i].Length; k++)
  {
    Console.Write(myArray[i][k] + "\t");
  }
  Console.WriteLine();
}

Resalt is:

L       o       r       e       m
i       p       s       u       m
d       o       l       o       r
s       i       t       a       m
e       t       ▒       ▒       ▒

So, last array length is 5 (but should be - 2) and what the symbol is: '▒' ?

Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

享受孤独 2025-01-16 14:17:02

我已经修正了我的错误:

char[][] Foo(StreamReader stream, int arraySize)
{
    long arrLength;

    if (stream.BaseStream.Length % arraySize == 0)
    {
        arrLength = stream.BaseStream.Length / arraySize;
    }
    else
    {
        arrLength = (stream.BaseStream.Length / arraySize) + 1;
    }

    char[][] array = new char[arrLength][];

    for (int i = 0; i < array.Length; i++)
    {
        long chunkSize = arraySize;

        if (i == array.Length - 1 && stream.BaseStream.Length % arraySize != 0)
        {
            chunkSize = stream.BaseStream.Length % arraySize;
        }

        array[i] = new char[chunkSize];

        for (int j = 0; j < chunkSize; j++)
        {
            array[i][j] = (char)stream.Read();
        }
    }

    return array;
}

I've fixed my error:

char[][] Foo(StreamReader stream, int arraySize)
{
    long arrLength;

    if (stream.BaseStream.Length % arraySize == 0)
    {
        arrLength = stream.BaseStream.Length / arraySize;
    }
    else
    {
        arrLength = (stream.BaseStream.Length / arraySize) + 1;
    }

    char[][] array = new char[arrLength][];

    for (int i = 0; i < array.Length; i++)
    {
        long chunkSize = arraySize;

        if (i == array.Length - 1 && stream.BaseStream.Length % arraySize != 0)
        {
            chunkSize = stream.BaseStream.Length % arraySize;
        }

        array[i] = new char[chunkSize];

        for (int j = 0; j < chunkSize; j++)
        {
            array[i][j] = (char)stream.Read();
        }
    }

    return array;
}
念﹏祤嫣 2025-01-16 14:17:02

如果您使用 .NET6,则可以使用 Chunk 方法将输入拆分为块:

char[][] Foo(TextReader stream, int arraySize) 
{
    return stream.ReadToEnd().Chunk(arraySize).ToArray();
}

如果这不可能,您可以将当前块的大小计算为 Math.Min(arraySize, array.Length - i * arraySize) 并在 for (int i...) 循环中使用它代替 arraySize。 (array.Length - i * arraySize 是剩余长度):

for (int i = 0; i < array.Length; i++)
{
    int chunkSize = Math.Min(arraySize, streamReader.BaseStream.Length - i * arraySize);
    array[i] = new char[chunkSize];

    for (int j = 0; j < chunkSize; j++)
    {
        array[i][j] = (char)streamReader.Read();
    }
}

If you are on .NET6, you can make use of the Chunk method to split the input into chunks:

char[][] Foo(TextReader stream, int arraySize) 
{
    return stream.ReadToEnd().Chunk(arraySize).ToArray();
}

If this is not possible, you may calculate the size of the current chunk as Math.Min(arraySize, array.Length - i * arraySize) and use this instead of arraySize in your for (int i...) loop. (array.Length - i * arraySize is the remaining length):

for (int i = 0; i < array.Length; i++)
{
    int chunkSize = Math.Min(arraySize, streamReader.BaseStream.Length - i * arraySize);
    array[i] = new char[chunkSize];

    for (int j = 0; j < chunkSize; j++)
    {
        array[i][j] = (char)streamReader.Read();
    }
}
千纸鹤带着心事 2025-01-16 14:17:02

这可能不是最好的方法,但为了简单起见,您可以创建一个 List ,然后使用 StreamReader.ReadBlock() 填充缓冲区。

然后,根据该块中读取的字符数(通过 .ReadBlock() 方法方便地以 int 形式返回),实例化一个正确大小的数组并将其添加到列表中,使用最后的 .ToArray() 生成您的 char[][]

    char[][] Foo(StreamReader streamReader, int arraySize)
    {
        List<char[]> listOfArrays = new List<char[]>();
        char[] charBuffer = new char[arraySize];
        int charactersInThisBlock;

        while (!streamReader.EndOfStream)
        {
            //fills the charBuffer with as many characters as possible up to its' limit (arraySize)
            //the second parameter (0) simply keeps the function reading from the last unread character
            //the output will tell you the number of characters read into the buffer
            charactersInThisBlock = streamReader.ReadBlock(charBuffer, 0, arraySize);

            //create/fill your new array, and add it to the list
            char[] newArray = new char[charactersInThisBlock];
            for(int i = 0; i < charactersInThisBlock; i++)
            {
                newArray[i] = charBuffer[i];
            }

            listOfArrays.Add(newArray);
        }

        //return char[][]
        return listOfArrays.ToArray();
    }

This might not be the best way, but for simplicity's sake you can just make a List<char[]> and then use StreamReader.ReadBlock() to fill a buffer.

Then, based on the number of characters read in that block (conveniently returned as an int by the .ReadBlock() method), you instantiate an array of the correct size and add it to the list, using .ToArray() at the end to produce your char[][].

    char[][] Foo(StreamReader streamReader, int arraySize)
    {
        List<char[]> listOfArrays = new List<char[]>();
        char[] charBuffer = new char[arraySize];
        int charactersInThisBlock;

        while (!streamReader.EndOfStream)
        {
            //fills the charBuffer with as many characters as possible up to its' limit (arraySize)
            //the second parameter (0) simply keeps the function reading from the last unread character
            //the output will tell you the number of characters read into the buffer
            charactersInThisBlock = streamReader.ReadBlock(charBuffer, 0, arraySize);

            //create/fill your new array, and add it to the list
            char[] newArray = new char[charactersInThisBlock];
            for(int i = 0; i < charactersInThisBlock; i++)
            {
                newArray[i] = charBuffer[i];
            }

            listOfArrays.Add(newArray);
        }

        //return char[][]
        return listOfArrays.ToArray();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文