c# - 无论如何,都不会读完文字然后写C#

标签 c#

我正在尝试完成大学作业,但我不知道编码出了什么问题。我应该获取文本文件,通读它并使用模型计数器输出标题、总重量、总高度,同时它会排序并写出符合分配标准的模型。它应该类似于这样:

姓名性别体重高度眼睛颜色头发颜色

Jane Doe F 135 5'8 蓝色金发女郎

总重量:135磅

总高度:5'8

型号:1

这是我到目前为止所写的内容,但它甚至不会写标题。我也写了很多方法,我什至拿了教授的代码并进行了调整。我正在 MacOS 上使用 VisualStudio 进行编码。

using System;
using System.IO;

namespace ModelRoster
{
    class Program
    {
        static void Main()
        {

            StreamReader sr = new StreamReader(@"/Users/jennie/Desktop/Computer Programming/Assignment 4/Models.txt");
            StreamWriter sw = new StreamWriter(@"/Users/jennie/Desktop/Computer Programming/Assignment 4/Payroll.txt");
            string line;

            //input
            string Name;
            int Weight;
            int Height;
            char Eyes = new char();
            char Hair = new char();
            char Gender = new char();

            int TotalW = 0;
            int TotalH = 0;
            int Mdls = 0;

            //misc
            int MWidth = 5;
            int MLength = 0;
            string Title;
            int End = 0;

            //heading
            Title = ("Name\t\tGender\t\tWeight\t\tHeight\t\tEye Color\t\tHair Color");
            MLength = Title.Length;
            End = (MWidth - MLength) / 2 + MLength;
            sw.WriteLine(Title.PadLeft(End));
            //
            sw.WriteLine(" ");

            //output
            while ((line = sr.ReadLine()) != null)
            {

            }
        }
    }
}

最佳答案

使用 block 包装您的流,以确保它们在您完成使用后关闭。这也将确保任何打开的流编写器在关闭时都会被刷新。

using(StreamReader sr = new StreamReader(@"/Users/jennie/Desktop/Computer Programming/Assignment 4/Models.txt"))
using(StreamWriter sw = new StreamWriter(@"/Users/jennie/Desktop/Computer Programming/Assignment 4/Payroll.txt"))
{
  // your code here
}

关于c# - 无论如何,都不会读完文字然后写C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43925192/

相关文章:

C# Get 访问器不可访问

c# - 我可以避免在从 iPhoneOSGameView 派生的 View 中导出 LayerClass 吗?

c# - Windows Phone 8 中的设备方向

c# - 使用调解器模式进行单元测试 - 所有私有(private)到公共(public)

c# 将数组列表写入文本文件

c# - 如何在保持其大小的同时自动将 WPF 窗口捕捉到屏幕边缘?

c# - 为集成测试创建默认的 HttpClientFactory

c# - Linq2SQL,异常后使用dataContext

c# - 未找到 MaxLength DataAnnontation

c# - 将图像从 iPhone 上传到 WCF 服务