file of struct's, array of struct's in mem == list in C#?

G

Glenn

OK, need help in translating what I'd like to do in old school C to
best method C#

If I was wanting to create an array of struct's, writing and reading
them to/from a file.. how would I do this in C#?

I was thinking, in so far as in memory, it would be a list of the
struct..?

For writing to file though, if I do a write of the struct, it's not
working correctly. Should I be using something other than StreamWriter
here?

Thanks for any help :)
Glenn
 
N

Nicholas Paldino [.NET/C# MVP]

Glenn,

Why not just use a binary formatter and serialization? Or do you have a
requirement to read files that were already persisted from another
application with a custom format?
 
G

Glenn

Nicholas,

Thank you for your response. Why don't I use them? Cause I don't know
what I'm doing :D hehe

I'm an old C guy that's trying to covert his C code to C# to be able
to eventually create a windows form application. Right now I'm just
trying to figure out things that are done differently here in C#.

If you wouldn't mind giving me a short code example that I can
understand what you're doing. One you give me the example I'll look up
the stuff in MS Help (How do I/ Search).

Currently I'm converting the structs to a string and writing them to
file (since I couldn't figure out who to write the structs) and
figured that there's got to be a better way. So was just hoping for a
nudge in the correct direction :)

Thanks for your help
Glenn
 
D

DeveloperX

Glenn,

Why not just use a binary formatter and serialization? Or do you have a
requirement to read files that were already persisted from another
application with a custom format?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




OK, need help in translating what I'd like to do in old school C to
best method C#
If I was wanting to create an array of struct's, writing and reading
them to/from a file.. how would I do this in C#?
I was thinking, in so far as in memory, it would be a list of the
struct..?
For writing to file though, if I do a write of the struct, it's not
working correctly. Should I be using something other than StreamWriter
here?
Thanks for any help :)
Glenn- Hide quoted text -

- Show quoted text -

I get quite confused by the variety of writers/readers/streams and
formatters. Here's a tiny example which will do what you want, but a)
it's 1.1 code and b) I'm not sure it's using the most appropriate bits
of the framework. Watch for line wrap, I fully qualified everything
while I hunted through the framework looking for the relevant bits.
All the ISerializable stuff on the struct is entirely optional.

using System;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.IO;

namespace WindowsApplication13
{
[Serializable]
struct AStruct : ISerializable
{
public AStruct(int px, string ps)
{
x=px;
s=ps;
}
public AStruct(SerializationInfo information, StreamingContext
context)
{
x=(int)information.GetValue("x",typeof(int));
s=information.GetString("s");
}
public int x;
public string s;
#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext
context)
{
info.AddValue("x",x);
info.AddValue("s",s);
}

#endregion
}
public class Class2
{
public Class2()
{}
public void WriteToFile()
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.Collections.ArrayList al = new
System.Collections.ArrayList();
al.Add(new AStruct(1,"test1"));
al.Add(new AStruct(2,"test2"));
al.Add(new AStruct(3,"test3"));
al.Add(new AStruct(4,"test4"));
using(System.IO.FileStream fs = new FileStream(@"C:
\out.bin",System.IO.FileMode.Create))
{
bf.Serialize(fs,al);
fs.Close();
}
}
public void ReadFromFile()
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.Collections.ArrayList al = new
System.Collections.ArrayList();
using(System.IO.FileStream fs = new FileStream(@"C:
\out.bin",System.IO.FileMode.Open))
{
System.IO.BinaryReader br = new BinaryReader(fs);
al = (System.Collections.ArrayList)bf.Deserialize(fs);
fs.Close();
}
Console.WriteLine(al.Count);
}
}
}
 
B

Ben Voigt

Glenn said:
Nicholas,

Thank you for your response. Why don't I use them? Cause I don't know
what I'm doing :D hehe

I'm an old C guy that's trying to covert his C code to C# to be able
to eventually create a windows form application. Right now I'm just
trying to figure out things that are done differently here in C#.

Don't convert. Use what you know, add .NET where it makes sense (for your
GUI).

C++/CLI, available in Visual Studio 2005, all versions, including Express.
 
G

Glenn

Don't convert. Use what you know, add .NET where it makes sense (for your
GUI).

C++/CLI, available in Visual Studio 2005, all versions, including Express.

Sorry for delay in re'ing.. have been helping out a friend's business.

Can you explain what you mean? I thought I used to some native C
stuff, but seems that the compiler is always complaining.. do I have
to do some kind of setting?

BTW: I am actually converting.. cause I originally wrote this app in
PHP using MySQL, however it's horribly slow. That's why I'm switching
over. :p

Thanks
Glenn
 
G

Glenn

Glenn,

Why not just use a binary formatter and serialization? Or do you have a
requirement to read files that were already persisted from another
application with a custom format?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




OK, need help in translating what I'd like to do in old school C to
best method C#
If I was wanting to create an array of struct's, writing and reading
them to/from a file.. how would I do this in C#?
I was thinking, in so far as in memory, it would be a list of the
struct..?
For writing to file though, if I do a write of the struct, it's not
working correctly. Should I be using something other than StreamWriter
here?
Thanks for any help :)
Glenn- Hide quoted text -

- Show quoted text -

I get quite confused by the variety of writers/readers/streams and
formatters. Here's a tiny example which will do what you want, but a)
it's 1.1 code and b) I'm not sure it's using the most appropriate bits
of the framework. Watch for line wrap, I fully qualified everything
while I hunted through the framework looking for the relevant bits.
All the ISerializable stuff on the struct is entirely optional.

using System;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.IO;

namespace WindowsApplication13
{
[Serializable]
struct AStruct : ISerializable
{
public AStruct(int px, string ps)
{
x=px;
s=ps;
}
public AStruct(SerializationInfo information, StreamingContext
context)
{
x=(int)information.GetValue("x",typeof(int));
s=information.GetString("s");
}
public int x;
public string s;
#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext
context)
{
info.AddValue("x",x);
info.AddValue("s",s);
}

#endregion
}
public class Class2
{
public Class2()
{}
public void WriteToFile()
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.Collections.ArrayList al = new
System.Collections.ArrayList();
al.Add(new AStruct(1,"test1"));
al.Add(new AStruct(2,"test2"));
al.Add(new AStruct(3,"test3"));
al.Add(new AStruct(4,"test4"));
using(System.IO.FileStream fs = new FileStream(@"C:
\out.bin",System.IO.FileMode.Create))
{
bf.Serialize(fs,al);
fs.Close();
}
}
public void ReadFromFile()
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.Collections.ArrayList al = new
System.Collections.ArrayList();
using(System.IO.FileStream fs = new FileStream(@"C:
\out.bin",System.IO.FileMode.Open))
{
System.IO.BinaryReader br = new BinaryReader(fs);
al = (System.Collections.ArrayList)bf.Deserialize(fs);
fs.Close();
}
Console.WriteLine(al.Count);
}
}
}

Sorry for delay in re'ing.. have been helping out a friend's business.

So, I was looking at List myself, I see you're using ArrayList... much
difference?

I know there's going to be close to a million records. How is the
finding/searching?
Saw something about the .sort and .binarysearch. Do I have to resort
after every .add? or will it keep it sorted once I specify .sort?

Thanks again for all your help :)
Glenn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top