Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. read and write in file

read and write in file

Scheduled Pinned Locked Moved C#
questionsalestutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tek 2009
    wrote on last edited by
    #1

    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????

    A R 2 Replies Last reply
    0
    • T tek 2009

      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????

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      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.

      T 1 Reply Last reply
      0
      • A Abhinav S

        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.

        T Offline
        T Offline
        tek 2009
        wrote on last edited by
        #3

        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

        A 1 Reply Last reply
        0
        • T tek 2009

          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

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #4

          You will need to write your own logic for this (once you decide on the structure of your text file).

          1 Reply Last reply
          0
          • T tek 2009

            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????

            R Offline
            R Offline
            riced
            wrote on last edited by
            #5

            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 Vicious

            membre123 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.

            T 1 Reply Last reply
            0
            • R riced

              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 Vicious

              membre123 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.

              T Offline
              T Offline
              tek 2009
              wrote on last edited by
              #6

              think you very mutch, that what's i need :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups