read and write in file
-
how to fill a standard list dictionary <> from a text file????
Dictionary<int, Customer> customers = new Dictionary<int, Customer>();
Customer cust1 = new Customer();
customers.Add(cust1.ID, cust1);
customers.Add(cust2.ID, cust1);
customers.Add(cust3.ID, cust1);and what is the other command to read and write in a small file size to share filestream????
-
how to fill a standard list dictionary <> from a text file????
Dictionary<int, Customer> customers = new Dictionary<int, Customer>();
Customer cust1 = new Customer();
customers.Add(cust1.ID, cust1);
customers.Add(cust2.ID, cust1);
customers.Add(cust3.ID, cust1);and what is the other command to read and write in a small file size to share filestream????
membre123 wrote:
how to fill a standard list dictionary <> from a text file????
You will need to parse your text file to get a key value pair and then add them to your dictionary. Using an xml file for this might actually be better than a text file. To read and write a file, you should go to msdn.
-
membre123 wrote:
how to fill a standard list dictionary <> from a text file????
You will need to parse your text file to get a key value pair and then add them to your dictionary. Using an xml file for this might actually be better than a text file. To read and write a file, you should go to msdn.
-
thank you for your answer, if you can give me a quick example to analyze the file, and thank you, please I really need it
-
how to fill a standard list dictionary <> from a text file????
Dictionary<int, Customer> customers = new Dictionary<int, Customer>();
Customer cust1 = new Customer();
customers.Add(cust1.ID, cust1);
customers.Add(cust2.ID, cust1);
customers.Add(cust3.ID, cust1);and what is the other command to read and write in a small file size to share filestream????
membre123 wrote:
how to fill a standard list dictionary <> from a text file????
Here's a little sample - it has no error handling or anything nice about it.
using System;
using System.Collections.Generic;
using System.IO;namespace ConsoleApplication1
{
class Customer
{
public int ID;
public string name;
}class Program
{
static void Main(string[] args)
{
var Customers = new Dictionary<int, Customer>();
using (StreamReader fs = new StreamReader(@"C:\Stuff\theInputFile.txt"))
{
string dataLine;
while ((dataLine = fs.ReadLine()) != null)
{
string[] s = dataLine.Split(',');
int id = int.Parse(s[0]);
Customer c = new Customer();
c.ID = id;
c.name = s[1].Trim();
Customers.Add(c.ID, c);
}
}foreach (var item in Customers) { Console.WriteLine("ID: {0} Customer {1}", item.Key, item.Value.name ); } Console.ReadLine(); }
}
}What you have to do depends entirely on the declaration for Customer and the layout of the input file. My input file looks like this:
1, Jet Black
2, Hugh Cornwell
3, Joe Strummer
4, Sid Viciousmembre123 wrote:
and what is the other command to read and write in a small file size to share filestream????
I don't know what this means.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
membre123 wrote:
how to fill a standard list dictionary <> from a text file????
Here's a little sample - it has no error handling or anything nice about it.
using System;
using System.Collections.Generic;
using System.IO;namespace ConsoleApplication1
{
class Customer
{
public int ID;
public string name;
}class Program
{
static void Main(string[] args)
{
var Customers = new Dictionary<int, Customer>();
using (StreamReader fs = new StreamReader(@"C:\Stuff\theInputFile.txt"))
{
string dataLine;
while ((dataLine = fs.ReadLine()) != null)
{
string[] s = dataLine.Split(',');
int id = int.Parse(s[0]);
Customer c = new Customer();
c.ID = id;
c.name = s[1].Trim();
Customers.Add(c.ID, c);
}
}foreach (var item in Customers) { Console.WriteLine("ID: {0} Customer {1}", item.Key, item.Value.name ); } Console.ReadLine(); }
}
}What you have to do depends entirely on the declaration for Customer and the layout of the input file. My input file looks like this:
1, Jet Black
2, Hugh Cornwell
3, Joe Strummer
4, Sid Viciousmembre123 wrote:
and what is the other command to read and write in a small file size to share filestream????
I don't know what this means.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.