回 帖 发 新 帖 刷新版面

主题:求助:格式转换问题!!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CoordinateChange
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string rtkFileName = string.Empty;
        string datFileName = string.Empty;

        private void buttonOpenRTKFile_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBoxRtkFileName.Text = this.openFileDialog1.FileName;
                rtkFileName = this.openFileDialog1.FileName;
            }
        }

        private void buttonConvertScs_Click(object sender, EventArgs e)
        {
            if (rtkFileName == string.Empty || this.saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            this.textBoxDatFileName.Text = this.saveFileDialog1.FileName;
            this.datFileName = this.saveFileDialog1.FileName;
            StreamReader streamReader = new StreamReader(rtkFileName, System.Text.Encoding.Default);
            StreamWriter streamWriter = new StreamWriter(datFileName, false, System.Text.Encoding.Default);
            //streamWriter.AutoFlush = true;

            char[] split = new char[] { '\t', ' ', ',' };
            string[] colStr;
            int count = 0;           //计算读取文件中总的行数
            while (!streamReader.EndOfStream)
            {
                colStr = streamReader.ReadLine().Split(split);
                if (colStr.Length == 5)
                {
                    count++;
                }
            }

            streamReader.BaseStream.Position = 0;
            streamWriter.WriteLine(count);

            while (!streamReader.EndOfStream)
            {
                colStr = streamReader.ReadLine().Split(split);
                if (colStr.Length == 5)
                {
                    //streamWriter.WriteLine(colStr[0]+","+colStr[3]+","+colStr[2]+","+colStr[4]);
                    streamWriter.WriteLine(colStr[0]);
                    streamWriter.WriteLine(colStr[3]);
                    streamWriter.WriteLine(colStr[2]);
                    streamWriter.WriteLine(colStr[4]);
                    streamWriter.WriteLine();
                }
            }

            streamReader.Close();
            streamWriter.Close();
        }
    }
}


源文件A格式为:
L0 0000000 557.146 680.298 966.984
L1 0000000 9464.079 839.310 969.298
L2 0000000 462.461 840.370 969.247
L3 0000000 467.497 858.105 969.987
L4 0000000 469.736 858.497 970.008
L5 0000000 475.176 873.267 970.722

A转换后的格式为B:
6
L0
680.298
557.146
966.984

L1
839.310
464.079
969.298

L2
840.370
462.461
969.247

L3
858.105
467.497
969.987

L4
858.497
469.736
970.008

L5
873.267
475.176
970.722

现在如何再把B格式的文件再转换成A格式文件?请高手给与帮助,谢谢!!!

回复列表 (共3个回复)

沙发

class ccc
{
string name;
string a;
string b;
string c;
public string Name{set{...}get{...}}
public string A{set{...}get{...}}
public string B{set{...}get{...}}
public string C{set{...}get{...}}
public override string ToString()
{
return name+" 000000 "+b+" "+a+" "+c;
}
}

ccc Ins=new ccc();
......
streamReader.ReadLine();
int i=0;
while (!streamReader.EndOfStream)
{
string s= streamReader.ReadLine();
switch(i%5)
{
case 0:Ins.Name=s;break;
case 1:Ins.A=s;break;
case 2:Ins.B=s;break;
case 3:Ins.C=s;break;
case 4:Console.WriteLine(Ins.ToString());break;
default:break;
}i++;
}

板凳

谢谢liusuifeng
只是这里的set和get该如何应用,可否详细解释下,举个例子,谢谢!!!

3 楼

public string Name{set{this.name=value;}get{return this.name;}}

我来回复

您尚未登录,请登录后再回复。点此登录或注册