Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将文本从控制台输出到文本文件_C#_.net_Windows - Fatal编程技术网

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

C# 无法将文本从控制台输出到文本文件,c#,.net,windows,C#,.net,Windows,我正在尝试将文本写入文本文件: 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 =

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

        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]));

        }
byte[]b=新字节[100];
int k=s.Receive(b);
Console.WriteLine(“接收…”);
for(int i=0;i
虽然Console.Write方法工作得很好,文本输出显示在屏幕上,但当我使用TextWriter方法时,应用程序会产生100个错误(继续),并且不会向指定的输出文件输出任何内容

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

以下是错误示例:

服务器正在端口8111上运行。。。本地端点是 :172.16.0.37:8111正在等待连接。。。。。接受连接 从172.16.0.37:59307收到//错误。。。。。在 System.IO.\uu Error.WinIOError(Int32 errorCode,字符串maybeFullPath)
在System.IO.FileStream.Init(字符串路径、文件模式、文件访问 访问、Int32权限、布尔用户权限、文件共享共享、Int32 缓冲区大小,文件选项,安全属性,字符串 msgPath,Boolean bFromProxy)位于System.IO.FileStream..ctor(字符串 路径、文件模式、文件访问访问、文件共享共享、Int32 缓冲区大小、文件选项)位于 System.IO.StreamWriter.CreateFile(字符串路径,布尔追加)位于 System.IO.StreamWriter..ctor(字符串路径、布尔追加、编码 System.IO.StreamWriter..ctor(字符串)处的编码,Int32 bufferSize) 中的integraserver.Main()处 C:\Users\Limited\Desktop\IntegClient\IntegServer\IntegServer\Program.cs:line 38错误。。。。。位于System.Net.Sockets.Socket.DoBind(端点 端点快照,SocketAddress(SocketAddress)位于 位于的System.Net.Sockets.Socket.Bind(端点localEP) System.Net.Sockets.TcpListener.Start(Int32 backlog)位于 中IntegralServer.Main()处的System.Net.Sockets.TcpListener.Start() C:\Users\Limited\Desktop\IntegClient\IntegServer\IntegServer\Program.cs:line 21错误。。。。。位于System.Net.Sockets.Socket.DoBind(端点 端点快照,SocketAddress(SocketAddress)位于 位于的System.Net.Sockets.Socket.Bind(端点localEP) System.Net.Sockets.TcpListener.Start(Int32 backlog)位于 中IntegralServer.Main()处的System.Net.Sockets.TcpListener.Start() C:\Users\Limited\Desktop\IntegClient\IntegServer\IntegServer\Program.cs:line 21错误。。。。。位于System.Net.Sockets.Socket.DoBind(端点 端点快照,SocketAddress(SocketAddress)位于 位于的System.Net.Sockets.Socket.Bind(端点localEP) System.Net.Sockets.TcpListener.Start(Int32 backlog)位于 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();
}
试试看
{
IPAddress ipAd=IPAddress.Parse(“172.16.0.37”);//使用本地m/c IP地址,并在客户端中使用相同的地址
/*初始化侦听器*/
TcpListener myList=新的TcpListener(ipAd,8111);
/*在指定的端口开始侦听*/
myList.Start();
WriteLine(“服务器正在8111端口运行…”);
WriteLine(“本地端点为:“+myList.LocalEndpoint”);
WriteLine(“等待连接…”);
套接字s=myList.AcceptSocket();
Console.WriteLine(“从“+s.RemoteEndPoint”接受连接);
字节[]b=新字节[100];
int k=s.Receive(b);
Console.WriteLine(“接收…”);
使用(var txt=File.OpenWrite(“output.txt”))
{
for(int i=0;i
也许这就是你想要做的

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]));
    }
}
byte[]b=新字节[100];
int k=s.Receive(b);
Console.WriteLine(“接收…”);
使用(var sw=new StreamWriter(“output.txt”))
{
for(int i=0;i
也许这就是你想要做的

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]));
    }
}
byte[]b=新字节[100];
int k=s.Receive(b);
Console.WriteLine(“接收…”);
使用(var sw=new StreamWriter(“output.txt”))
{
for(int i=0;i
看来您的问题出在访问模式中。您可以更改代码以创建以访问模式/写入或读写模式传递的文件流吗。。 下面是一个例子,如果你需要遵循

FileStream fsFileStream = null;
fsFileStream = new FileStream("yourOutputFile", FileMode.Open, FileAccess.ReadWrite);
// then add your code here.. also is there a reason why you are declaring such a small byte to write try 512 or 1024 just a suggestion

看起来您的问题在访问模式中。您可以更改代码以创建以访问模式/写入或读写模式传递的文件流吗。。 下面是一个例子,如果你需要遵循

FileStream fsFileStream = null;
fsFileStream = new FileStream("yourOutputFile", FileMode.Open, FileAccess.ReadWrite);
// then add your code here.. also is there a reason why you are declaring such a small byte to write try 512 or 1024 just a suggestion

错误是什么?(可能只是其中的一个子集…)使用
TextWriter
时会出现哪些错误?是否在循环中创建新的编写器??那是在找麻烦。B知道了,但他删除了他的答案。。他的答案是add:File.writealBytes(“output.txt”,b);仍然在看错误,这似乎是一个权利问题,以及..什么是错误?(可能只是其中的一个子集…)使用
TextWriter
时会出现哪些错误?是否在循环中创建新的编写器??那是在找麻烦。B知道了,但他删除了他的答案。。他的答案是add:File.writealBytes(“output.txt”,