C# “阅读流的内容”;溪流;使用;ReadToEnd();在C中#
我有一个函数,它有一个类型为“stream”的输入参数。 我想读取此流的内容并与文件的内容进行比较。 已经做了以下工作:C# “阅读流的内容”;溪流;使用;ReadToEnd();在C中#,c#,stream,C#,Stream,我有一个函数,它有一个类型为“stream”的输入参数。 我想读取此流的内容并与文件的内容进行比较。 已经做了以下工作: public bool func(Stream stream, string tempFilePath, string dstFilePath) { string[] File1 = File.ReadAllLines(tempFilePath); stream.Seek(0, SeekOrigin.Begin); int line = 0;
public bool func(Stream stream, string tempFilePath, string dstFilePath)
{
string[] File1 = File.ReadAllLines(tempFilePath);
stream.Seek(0, SeekOrigin.Begin);
int line = 0;
StreamReader reader = new StreamReader(stream);
string[] File2 = reader.ReadToEnd().Split(
new[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);
while (line < File1.Length)
{
if (astFile2[line] != astFile1[line])
break;
line++;
}
bool ret = line == astFile1.Length && line == astFile2.Length - 1;
if (ret )
{
using (var dstFile = new FileStream(dstFilePath, FileMode.Create, FileAccess.Write))
{
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(dstFile );
}
}
return ret;
}
“File3”的内容仍然为空 我总是确保在执行ReadToEnd()之前调用stream.Flush()。我总是确保在执行ReadToEnd()之前调用stream.Flush()。您似乎没有向我们显示实际的错误代码 我已将您的代码转述到一个可编译的控制台应用程序中,该应用程序不会重现问题:
using System.IO;
namespace Demo
{
public static class Program
{
static void Main()
{
string sourceFile = @"D:\tmp\source.txt";
string destFile = @"D:\tmp\dest.txt";
MemoryStream ms = new MemoryStream();
var data = File.ReadAllBytes(sourceFile);
ms.Write(data, 0, data.Length);
ms.Position = 0;
func(ms, sourceFile, destFile);
}
public static void func(Stream stream, string tempFilePath, string dstFilePath)
{
File.ReadAllLines(tempFilePath);
stream.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(stream);
reader.ReadToEnd();
stream.Position = 0;
using (var dstFile = new FileStream(dstFilePath, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(dstFile);
}
}
}
}
所以我担心“这段代码遗漏了什么?”这个问题的答案是“一个可复制的问题”
您必须告诉我们要传递给
func()
的流的具体类型。这很可能是您的问题的原因-例如,它是一个只能读取一次的流(尽管在这种情况下,尝试更改流位置确实会抛出一个不支持的操作异常
)。您似乎没有向我们显示实际的错误代码
我已将您的代码转述到一个可编译的控制台应用程序中,该应用程序不会重现问题:
using System.IO;
namespace Demo
{
public static class Program
{
static void Main()
{
string sourceFile = @"D:\tmp\source.txt";
string destFile = @"D:\tmp\dest.txt";
MemoryStream ms = new MemoryStream();
var data = File.ReadAllBytes(sourceFile);
ms.Write(data, 0, data.Length);
ms.Position = 0;
func(ms, sourceFile, destFile);
}
public static void func(Stream stream, string tempFilePath, string dstFilePath)
{
File.ReadAllLines(tempFilePath);
stream.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(stream);
reader.ReadToEnd();
stream.Position = 0;
using (var dstFile = new FileStream(dstFilePath, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(dstFile);
}
}
}
}
所以我担心“这段代码遗漏了什么?”这个问题的答案是“一个可复制的问题”
您必须告诉我们要传递给func()
的流的具体类型。这很可能是问题的原因-例如,它是一个只能读取一次的流(尽管在这种情况下,尝试更改流位置确实会抛出一个不支持的操作异常
)。您使用了reader.ReadToEnd()
将流指针移动到末尾,在调用stream.CopyTo()
之前,您没有将其重置为开始。正如我在文章中提到的,使用stream.Position=0没有帮助,因为您发布的代码中实际上没有。你为什么不公布你正在使用的实际代码?如果你必须根据问题不同部分中的一些注释在心里编辑代码,这会让阅读变得很困难……你已经在文件2
中有了流的内容-为什么你不写这些内容而不是重新阅读流?出于某些原因,我必须直接阅读内容。奇怪的是,在我上面描述的例子中,为什么不能对同一个流多次使用ReadToEnd()。您使用了将流指针移到末尾的reader.ReadToEnd()
,并且在调用stream.CopyTo()
之前没有将其重置为开始。正如我在文章中提到的,这没有帮助,通过使用stream.Position=0,您发布的代码中实际上没有这一点。你为什么不公布你正在使用的实际代码?如果你必须根据问题不同部分中的一些注释在心里编辑代码,这会让阅读变得很困难……你已经在文件2
中有了流的内容-为什么你不写这些内容而不是重新阅读流?出于某些原因,我必须直接阅读内容。奇怪的是,在我上面描述的例子中,为什么不能对同一个流多次使用ReadToEnd()。流是由一个我无权访问的函数创建的,但它是一个memorystream,我没有得到UnsupportedOperationException。这就是为什么我认为可以多次阅读的原因。我已经发布了我使用过的确切函数,并且发现了问题。我已经发布了我使用过的确切函数
-不,您没有。您发布的代码无法编译。例如,lineNum
没有在任何地方定义。流是由我无权访问的函数创建的,但它是一个memorystream,我没有得到UnsupportedOperationException。这就是为什么我认为可以多次阅读的原因。我已经发布了我使用过的确切函数,并且发现了问题。我已经发布了我使用过的确切函数
-不,您没有。您发布的代码无法编译。例如,lineNum
没有在任何地方定义。