程序员家园论坛软件开发.NET开发语言 请教!!.net读取txt文件并转化赋值的问题!

1  /  1  页   1 跳转 查看:752

请教!!.net读取txt文件并转化赋值的问题!

请教!!.net读取txt文件并转化赋值的问题!

在.net环境下,TXT数据文件形式如下:
23.1  34.5  56.7  43.7
23.1  34.5  56.7  43.7
23.1  34.5  56.7  43.7
23.1  34.5  56.7  43.7
23.1  34.5  56.7  43.7

需要按结构
struct str
{
double a;
double b;
double c;
double d;
}
逐行读取,并依次赋值给申请的(TXT文件数据的行数*sizeof(str))这么大结构数组里!我使用了托管的机制,好像不能实现直接的结构赋值和(struct*)molloc(行数*siazeof(str))对内存的申请!~希望高手好心朋友帮忙解决一下啊!先感谢!!!
 

回复:请教!!.net读取txt文件并转化赋值的问题!

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;using System.Runtime.InteropServices;namespace RWFile{public partial class Form1 : Form{public Form1()//从文件中读结构体private void button1_Click(object sender, EventArgs e){string strFile = Application.StartupPath + "test.dat";if (!File.Exists(strFile))FileStream fs = new FileStream(strFile, FileMode.Open,FileAccess.ReadWrite);TestStruct ts = new TestStruct();byte[] bytData = new byte[Marshal.SizeOf(ts)];fs.Read(bytData, 0, bytData.Length);fs.Close();ts = rawDeserialize(bytData);textBox1.Text = ts.dTest.ToString();textBox2.Text = ts.uTest.ToString();textBox3.Text = Encoding.Default.GetString(ts.bTest); }//向文件中写结构体private void button2_Click(object sender, EventArgs e){string strFile = Application.StartupPath + "test.dat";FileStream fs = new FileStream(strFile, FileMode.Create , FileAccess.Write);TestStruct ts = new TestStruct();ts.dTest = double.Parse(textBox1.Text);ts.uTest = UInt16.Parse(textBox2.Text);ts.bTest = Encoding.Default.GetBytes(textBox3.Text); byte[] bytData = rawSerialize(ts);fs.Write(bytData, 0, bytData.Length);fs.Close();}[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)] //,Size=16public struct TestStruct{[MarshalAs(UnmanagedType.R8)] //,FieldOffset(0)]  public double dTest;[MarshalAs(UnmanagedType.U2)] //, FieldOffset(8)]public UInt16 uTest;[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] //, FieldOffset(10)]public byte[] bTest;}//序列化public static byte[] rawSerialize(object obj){int rawsize = Marshal.SizeOf(obj);IntPtr buffer = Marshal.AllocHGlobal(rawsize);Marshal.StructureToPtr(obj, buffer, false);byte[] rawdatas = new byte[rawsize];Marshal.Copy(buffer, rawdatas, 0, rawsize);Marshal.FreeHGlobal(buffer);return rawdatas;}//反序列化public static TestStruct rawDeserialize(byte[] rawdatas){Type anytype = typeof(TestStruct);int rawsize = Marshal.SizeOf(anytype);if (rawsize > rawdatas.Length) return new TestStruct();IntPtr buffer = Marshal.AllocHGlobal(rawsize);Marshal.Copy(rawdatas, 0, buffer, rawsize);object retobj = Marshal.PtrToStructure(buffer, anytype);Marshal.FreeHGlobal(buffer);return (TestStruct)retobj;}        }}
暂时没有想好
 

回复:请教!!.net读取txt文件并转化赋值的问题!

fna = Server.MapPath("路径");
        if (!System.IO.File.Exists(fna))
        {
            LabelMsg1.Text = "指定文件不存在";
        }
        else
        {
            string[] line = new string[3];
            line = System.IO.File.ReadAllLines(fna);
        }

然后呢,文件里的全部内容都在line数组里了,总行数是line.Length,第一行是line[0],第二行是line[1],用循环就可以读出每一行

如果你要分割每一行的内容,可以用Split方法,你想把line数组里的数据保存到哪都可以

嗯,我不知道是不是看懂你的题目了,就是,C#里没有指针
 

回复: 请教!!.net读取txt文件并转化赋值的问题!



引用:
原帖由 mountain315 于 2008-5-8 8:02:00 发表
fna = Server.MapPath("路径");
        if (!System.IO.File.Exists(fna))
        {
            LabelMsg1.Text = "指定文件不存在";
        }
        else
        {
            string[] 

c#中是有指针的,不过是非托管的
暂时没有想好
 

回复:请教!!.net读取txt文件并转化赋值的问题!

哦,我一直以为没有指针,托管非托管,都搞不清
 
1  /  1  页   1 跳转

版权所有 程序员家园论坛   Sitemap

Powered by Discuz!NT 2.1.202    Copyright © 2001-2008 Comsenz Inc.
Processed in 0.046875 second(s) , 3 queries. 浙ICP备07502118号
返顶部