473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Apr 3 '07 #1
6 1987
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]
- mv*@spam.guard.caspershouse.com

"Glenn" <ba******@frontiernet.netwrote in message
news:bo********************************@4ax.com...
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

Apr 3 '07 #2
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
On Tue, 3 Apr 2007 11:00:23 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.comwrote:
>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?
Apr 3 '07 #3
On 3 Apr, 16:00, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

"Glenn" <bagsm...@frontiernet.netwrote in message

news:bo********************************@4ax.com...
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.Bin aryFormatter bf =
new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
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.Bin aryFormatter bf =
new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
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);
}
}
}

Apr 3 '07 #4

"Glenn" <ba******@frontiernet.netwrote in message
news:pd********************************@4ax.com...
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.
Apr 4 '07 #5
On Wed, 4 Apr 2007 08:31:41 -0500, "Ben Voigt" <rb*@nospam.nospam>
wrote:
>
"Glenn" <ba******@frontiernet.netwrote in message
news:pd********************************@4ax.com.. .
>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.
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
Apr 5 '07 #6
On 3 Apr 2007 08:29:14 -0700, "DeveloperX" <nn*****@operamail.com>
wrote:
>On 3 Apr, 16:00, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>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]
- m...@spam.guard.caspershouse.com

"Glenn" <bagsm...@frontiernet.netwrote in message

news:bo********************************@4ax.com.. .
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.Bin aryFormatter bf =
new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
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.Bin aryFormatter bf =
new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
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
Apr 5 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas | last post by:
What's the quickest way to write and read 10.000 integer values ( or more ) to and from a file? Using struct somehow? The example in the docs shows how to handle to or three arguments, but is the...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
11
by: Ignacio X. Domínguez | last post by:
Hi. I'm developing a desktop application that needs to store some data in a local file. Let's say for example that I want to have an address book with names and phone numbers in a file. I would...
2
by: Tiger | last post by:
I try to write a struct into a brut file but I can't write all var in this struct when I try to add user I have that : > testmachine:/usr/share/my_passwd# ./my_passwd -a users.db > Ajout d'une...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
6
by: chris237 | last post by:
I'm having trouble with the following program. i want it to have the options to save a list of data entered by the user and display it the next time they run it. Could someone please either show me a...
1
by: Skyer | last post by:
How to write? I have write program in C language. This program contain 1 array with struct. Every element of table must be dynamic list LIFO (stack). Adding and removing struct variable from...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.