線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:4363
推到 Plurk!
推到 Facebook!

請問如何用C#的struct去mapping一個txt file的一筆record

答題得分者是:shieh2700
carl168
一般會員


發表:7
回覆:16
積分:4
註冊:2004-02-18

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-12-07 14:36:44 IP:60.250.xxx.xxx 訂閱
請問各位先進,
C# 可以用 struct 去 mapping 一個 txt file 的一筆 record 嗎?
謝謝!
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-12-11 00:40:03 IP:61.64.xxx.xxx 訂閱
1.可以
2.不論任何語言都可以
3. 請去看資料結構的書,這種書,書局很多,可以徹底解決你的疑問

===================引 用 carl168 文 章===================
請問各位先進,
C# 可以用 struct 去 mapping 一個 txt file 的一筆 record 嗎?
謝謝!
HikaruGo
中階會員


發表:22
回覆:69
積分:88
註冊:2007-12-09

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-12-11 12:22:33 IP:61.62.xxx.xxx 訂閱
   好像有點難

Structs have the following properties:
  • Structs are value types and classes are reference types.
  • Unlike classes, structs can be instantiated without using a new operator.
  • Structs can declare constructors, but they must take parameters.
  • A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.
  • A struct can implement interfaces.
  • A struct can be used as a nullable type and can be assigned a null value.
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-12-11 13:17:03 IP:210.66.xxx.xxx 訂閱
其實也沒有那麼難,在觀念上
只是兩塊記憶體之間的搬移而已...

[code cpp]
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
struct MyStruct{
char a[2];
char b[3];
char c[4];
char d[5];
char e[6];
char f[7];
};

MyStruct st;

//null-terminated string fields
char *r = "A\0BB\0CCC\0DDDD\0EEEEE\0FFFFFF\0";

memset(&st,0,sizeof(st));
memcpy(&st,(char*)r,sizeof(st));

ListBox1->Clear();
ListBox1->Items->Add(st.a);
ListBox1->Items->Add(st.b);
ListBox1->Items->Add(st.c);
ListBox1->Items->Add(st.d);
ListBox1->Items->Add(st.e);
ListBox1->Items->Add(st.f);


}
//---------------------------------------------------------------------------
[/code]
編輯記錄
jow 重新編輯於 2007-12-11 13:17:31, 註解 無‧
jow 重新編輯於 2007-12-11 13:24:30, 註解 無‧
HikaruGo
中階會員


發表:22
回覆:69
積分:88
註冊:2007-12-09

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-12-11 22:47:01 IP:61.64.xxx.xxx 訂閱
C#可以嗎?
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#6 引用回覆 回覆 發表時間:2007-12-13 12:53:36 IP:210.66.xxx.xxx 訂閱
可以請問一下?!
(1)
在C#中可以宣告像以下的structure?
其中內含固定長度的資料欄位?

struct MyStruct{
char a[2];
char b[3];
char c[4];
char d[5];
char e[6];
char f[7];
};

(2)在C#中, 有沒有像memcpy()的函式
支援兩塊Buffer互相拷貝?

如果(1), (2)條件皆可滿足,那就可以做得到,
否則只能自行拆解輸入資料到各個structure 的欄位裡了

個人看法, 僅供參考...

===================引 用 HikaruGo 文 章===================
C#可以嗎?
HikaruGo
中階會員


發表:22
回覆:69
積分:88
註冊:2007-12-09

發送簡訊給我
#7 引用回覆 回覆 發表時間:2007-12-13 18:17:23 IP:61.62.xxx.xxx 訂閱
C# 不允許如此宣告
carl168
一般會員


發表:7
回覆:16
積分:4
註冊:2004-02-18

發送簡訊給我
#8 引用回覆 回覆 發表時間:2007-12-20 18:05:34 IP:60.250.xxx.xxx 訂閱
請教各位先進,
C# 真的不能做到這樣的功能嗎?
還是在這裡懂 C# 的先進不多?
shieh2700
高階會員


發表:0
回覆:127
積分:100
註冊:2002-06-13

發送簡訊給我
#9 引用回覆 回覆 發表時間:2007-12-25 12:23:39 IP:60.250.xxx.xxx 訂閱
使用序列化吧
carl168
一般會員


發表:7
回覆:16
積分:4
註冊:2004-02-18

發送簡訊給我
#10 引用回覆 回覆 發表時間:2007-12-26 15:26:09 IP:60.250.xxx.xxx 訂閱
shieh2700 先進您好,
感謝您的回覆,
但是不太了解您說的序列化,
可否再說得具體一點,
謝謝!
shieh2700
高階會員


發表:0
回覆:127
積分:100
註冊:2002-06-13

發送簡訊給我
#11 引用回覆 回覆 發表時間:2008-01-03 17:52:16 IP:118.168.xxx.xxx 訂閱

[code c#]
[StructLayout(LayoutKind.Sequential)]
public struct Rec1
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string Name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string Address;
public int Age;
public DateTime Birth;
};

public static class RawData
{
public static byte[] Serialize(object obj)
{
int rawSize = Marshal.SizeOf(obj);
IntPtr buffer = Marshal.AllocHGlobal(rawSize);
Marshal.StructureToPtr(obj, buffer, true);
byte[] result = new byte[rawSize];
Marshal.Copy(buffer, result, 0, rawSize);
Marshal.FreeHGlobal(buffer);
return result;
}

public static T Deserialize(byte[] rawData)
{
Type t = typeof(T);
int rawSize = Marshal.SizeOf(t);
IntPtr buffer = Marshal.AllocHGlobal(rawSize);
Marshal.Copy(rawData, 0, buffer, rawSize);
object result = Marshal.PtrToStructure(buffer, t);
Marshal.FreeHGlobal(buffer);
return (T)result;
}
}

[/code]



可參考下列這篇文章:
http://huanlin.dyndns.org/cs/blogs/huan-lins_blog/archive/2007/10/30/obfuscator-and-serialization.aspx


&

http://211.147.225.34/gate/big5/dotnet.chinaitlab.com/CSharp/735779.html
編輯記錄
shieh2700 重新編輯於 2008-01-04 15:16:03, 註解 無‧
shieh2700 重新編輯於 2008-01-04 17:45:01, 註解 無‧
系統時間:2024-04-20 21:16:02
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!