gpt4 book ai didi

c# - 无法将文本从控制台输出到文本文件

转载 作者:可可西里 更新时间:2023-11-01 10:49:52 26 4
gpt4 key购买 nike

我正在尝试将文本写入文本文件:

        byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
{
Console.Write(Convert.ToChar(b[i]));
//TextWriter tw = new StreamWriter("output.txt");
//tw.WriteLine(Convert.ToChar(b[i]));

}

虽然 Console.Write 方法完美运行,并且文本输出显示在屏幕上,但是当我使用 TextWriter 方法时,应用程序会产生 100 多个错误(继续)并且不会向指定的输出文件输出任何内容

是否有任何其他方式获取“Console.Write(Convert.ToChar(b[i]))”的输出;到文本文件?

以下是错误示例:

The server is running at port 8111... The local End point is :172.16.0.37:8111 Waiting for a connection..... Connection accepted from 172.16.0.37:59307 Recieved... //Error..... at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.StreamWriter.CreateFile(String path, Boolean append) at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) at System.IO.StreamWriter..ctor(String path) at IntegServer.Main() in C:\Users\Limited\Desktop\IntegClient\IntegServer\IntegServer\Program.cs:line 38 Error..... at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Bind(EndPoint localEP) at System.Net.Sockets.TcpListener.Start(Int32 backlog) at System.Net.Sockets.TcpListener.Start() at IntegServer.Main() in C:\Users\Limited\Desktop\IntegClient\IntegServer\IntegServer\Program.cs:line 21 Error..... at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Bind(EndPoint localEP) at System.Net.Sockets.TcpListener.Start(Int32 backlog) at System.Net.Sockets.TcpListener.Start() at IntegServer.Main() in C:\Users\Limited\Desktop\IntegClient\IntegServer\IntegServer\Program.cs:line 21 Error..... at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Bind(EndPoint localEP) at System.Net.Sockets.TcpListener.Start(Int32 backlog) at System.Net.Sockets.TcpListener.Start()

完整代码如下:

  try
{

IPAddress ipAd = IPAddress.Parse("172.16.0.37"); //use local m/c IP address, and use the same in the client

/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8111);

/* Start Listeneting at the specified port */
myList.Start();


Console.WriteLine("The server is running at port 8111...");
Console.WriteLine("The local End point is :" + myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");


Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
using (var txt = File.OpenWrite("output.txt"))
{
for (int i = 0; i < k; i++)
{
Console.Write(Convert.ToChar(b[i]));
txt.WriteLine(Convert.ToChar(b[i]));
}
}




ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement to Client");
/* clean up */
s.Close();
myList.Stop();


}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
// Loop the process
loop();
}

最佳答案

也许这就是您想要做的?

byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
using (var sw = new StreamWriter("output.txt"))
{
for (int i = 0; i < k; i++)
{
Console.Write(Convert.ToChar(b[i]));
sw.Write(Convert.ToChar(b[i]));
}
}

关于c# - 无法将文本从控制台输出到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8577937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com