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. Appropriate solution on reading a list of Strings from a file

Appropriate solution on reading a list of Strings from a file

Scheduled Pinned Locked Moved C#
jsonhelpquestion
8 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.
  • N Offline
    N Offline
    nstk
    wrote on last edited by
    #1

    Hallo, there is a List which has to be filled with Strings when the program starts up. I am missing the common practice in such cases, if there is any at all. I am thinking of putting all strings names in a text file from where the programme should read and fill up the List. That way, I will always be able to change it, by adding more names, changing or deleting them, avoiding the risk to mess up with the code. Are there any better suggestions on that? Would that be too slow if the List came at thousands of names? Should it be better to have a binary list of text strings, if something like that would be possible (through serialization maybe?) Those are some of the questions coming up, when I am thinking of the appropriate solution to the problem. Thanks.

    OriginalGriffO P 2 Replies Last reply
    0
    • N nstk

      Hallo, there is a List which has to be filled with Strings when the program starts up. I am missing the common practice in such cases, if there is any at all. I am thinking of putting all strings names in a text file from where the programme should read and fill up the List. That way, I will always be able to change it, by adding more names, changing or deleting them, avoiding the risk to mess up with the code. Are there any better suggestions on that? Would that be too slow if the List came at thousands of names? Should it be better to have a binary list of text strings, if something like that would be possible (through serialization maybe?) Those are some of the questions coming up, when I am thinking of the appropriate solution to the problem. Thanks.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      I would go with strings in a file, then just

      List<string> myList = new List<string>();
      myList.AddRange(File.ReadAllLines(path));

      The memory space shouldn't be a problem, even with thousands of strings, and it won't take long to load anyway. You could hold them as XML data, but then it is a bit harder to edit: you can cause errors in the file. At least with straight text, you don't need any special tools (or intelligence) to change them, so that job can be hived off to the office idiot junior. A quick test said mine read 5000 lines in 7mSec (on the first try, so caching was not involved) - and my PC is nowhere near SOTA! Always go with an option that means you don't have to re-compile!

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      N 1 Reply Last reply
      0
      • N nstk

        Hallo, there is a List which has to be filled with Strings when the program starts up. I am missing the common practice in such cases, if there is any at all. I am thinking of putting all strings names in a text file from where the programme should read and fill up the List. That way, I will always be able to change it, by adding more names, changing or deleting them, avoiding the risk to mess up with the code. Are there any better suggestions on that? Would that be too slow if the List came at thousands of names? Should it be better to have a binary list of text strings, if something like that would be possible (through serialization maybe?) Those are some of the questions coming up, when I am thinking of the appropriate solution to the problem. Thanks.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Griff gave a good answer, but I wonder at the wisdom of using a List rather than a database. Could you give more information on what you are trying to do?

        N 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          I would go with strings in a file, then just

          List<string> myList = new List<string>();
          myList.AddRange(File.ReadAllLines(path));

          The memory space shouldn't be a problem, even with thousands of strings, and it won't take long to load anyway. You could hold them as XML data, but then it is a bit harder to edit: you can cause errors in the file. At least with straight text, you don't need any special tools (or intelligence) to change them, so that job can be hived off to the office idiot junior. A quick test said mine read 5000 lines in 7mSec (on the first try, so caching was not involved) - and my PC is nowhere near SOTA! Always go with an option that means you don't have to re-compile!

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

          N Offline
          N Offline
          nstk
          wrote on last edited by
          #4

          Thanks for replying. What about having the text list in another form? Is there a way to compile it? Does this go under the serialization chapter?

          1 Reply Last reply
          0
          • P PIEBALDconsult

            Griff gave a good answer, but I wonder at the wisdom of using a List rather than a database. Could you give more information on what you are trying to do?

            N Offline
            N Offline
            nstk
            wrote on last edited by
            #5

            I want it to be a simple metric conversion programme, such as foot to meter and so on. My purpose is mainly an exercise for myself, but also to be able to share it with friends on a simple download. I cannot ask them to have a database already installed into their system, they wouldn't know how to do that.

            modified on Thursday, April 7, 2011 11:26 AM

            OriginalGriffO P 2 Replies Last reply
            0
            • N nstk

              I want it to be a simple metric conversion programme, such as foot to meter and so on. My purpose is mainly an exercise for myself, but also to be able to share it with friends on a simple download. I cannot ask them to have a database already installed into their system, they wouldn't know how to do that.

              modified on Thursday, April 7, 2011 11:26 AM

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Asnwering your comment to PIEBALDconsult and to me at teh same time: Not all databases need any installation. For example, there is SqlCE which was meant for Windows Mobile devices, but can be used by desktop machines. The code is built into .NET, so if they can run your program, it can use a database. It's pretty full SQL with only a few limitations: single user, no stored procedures, no triggers, but for 90% of simple apps it works like a charm - Outlook stores it's data in modified SqlCE databases. You can serialize it, yes:

                      // Read from file
                      string path = @"F:\\Temp\\roman.txt";
                      List myList = new List();
                      myList.AddRange(File.ReadAllLines(path));
                      // Write to serialization file.
                      using (Stream stream = File.Open(@"F:\\Temp\\data.bin", FileMode.Create))
                          {
                          BinaryFormatter bin = new BinaryFormatter();
                          bin.Serialize(stream, myList);
                          }
                      // Read from serialization file
                      List myList2;
                      using (Stream stream = File.Open(@"F:\\Temp\\data.bin", FileMode.Open))
                          {
                          BinaryFormatter bin = new BinaryFormatter();
                          myList2 = (List) bin.Deserialize(stream);
                          }
              

              But this will be slower than reading text! (Simple tests say about twice as slow!) Additionally, you will need some utility or method to modify values.

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              N 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Asnwering your comment to PIEBALDconsult and to me at teh same time: Not all databases need any installation. For example, there is SqlCE which was meant for Windows Mobile devices, but can be used by desktop machines. The code is built into .NET, so if they can run your program, it can use a database. It's pretty full SQL with only a few limitations: single user, no stored procedures, no triggers, but for 90% of simple apps it works like a charm - Outlook stores it's data in modified SqlCE databases. You can serialize it, yes:

                        // Read from file
                        string path = @"F:\\Temp\\roman.txt";
                        List myList = new List();
                        myList.AddRange(File.ReadAllLines(path));
                        // Write to serialization file.
                        using (Stream stream = File.Open(@"F:\\Temp\\data.bin", FileMode.Create))
                            {
                            BinaryFormatter bin = new BinaryFormatter();
                            bin.Serialize(stream, myList);
                            }
                        // Read from serialization file
                        List myList2;
                        using (Stream stream = File.Open(@"F:\\Temp\\data.bin", FileMode.Open))
                            {
                            BinaryFormatter bin = new BinaryFormatter();
                            myList2 = (List) bin.Deserialize(stream);
                            }
                

                But this will be slower than reading text! (Simple tests say about twice as slow!) Additionally, you will need some utility or method to modify values.

                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                N Offline
                N Offline
                nstk
                wrote on last edited by
                #7

                Thanks again for your answer and the nice piece of code. :)

                1 Reply Last reply
                0
                • N nstk

                  I want it to be a simple metric conversion programme, such as foot to meter and so on. My purpose is mainly an exercise for myself, but also to be able to share it with friends on a simple download. I cannot ask them to have a database already installed into their system, they wouldn't know how to do that.

                  modified on Thursday, April 7, 2011 11:26 AM

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Oh, one of those -- an excellent exercise. According to the file date, I wrote the following last October (probably in response to a post here):

                  namespace PIEBALD.Lib.LibExt.Convert
                  {
                  public interface IConvert
                  {
                  double Convert ( double Value ) ;
                  }

                  public enum Language
                  {
                      CSharp
                  ,
                      VisualBasic
                  }
                  
                  \[System.AttributeUsageAttribute(System.AttributeTargets.Field , AllowMultiple=false , Inherited=false)\]
                  public sealed class ConverterAttribute : System.Attribute
                  {
                      public ConverterAttribute
                      (
                          string   Function
                      ,
                          Language Language
                      )
                      {
                          this.Function = Function ;
                  
                          this.Language = Language ;
                  
                          return ;
                      }
                  
                      public string Function { get ; private set ; }
                  
                      public Language Language { get ; private set ; }
                  }
                  
                  public enum Conversion
                  {
                      \[ConverterAttribute("Value \* 2.54",Language.VisualBasic)\]
                      FromInchToCentimeter
                  ,
                      \[ConverterAttribute("Value / 2.54",Language.VisualBasic)\]
                      FromCentimeterToInch
                  ,
                      \[ConverterAttribute("( Value - 32.0 ) \* 5.0 / 9.0",Language.CSharp)\]
                      FromFahrenheitToCelsius
                  ,
                      \[ConverterAttribute("Value \* 9.0 / 5.0 + 32.0",Language.CSharp)\]
                      FromCelsiusToFahrenheit
                  }
                  
                  public static class LibExt
                  {
                      private static readonly System.Collections.Generic.Dictionary<Conversion,IConvert> conversion ;
                  
                      static LibExt
                      (
                      )
                      {
                          conversion = new System.Collections.Generic.Dictionary<Conversion,IConvert>() ;
                  
                          return ;
                      }
                  
                      private static void
                      Add
                      (
                          Conversion Conversion
                      )
                      {
                          System.Reflection.FieldInfo fi = typeof(Conversion).GetField
                          (
                              Conversion.ToString()
                          ,
                              System.Reflection.BindingFlags.Public
                              |
                              System.Reflection.BindingFlags.Static
                          ) ;
                  
                          foreach
                          (
                              ConverterAttribute att
                          in
                              fi.GetCustomAttributes ( typeof(ConverterAttribute) , false ) 
                          )
                          {
                              string code = null ;
                  
                              switch ( att.Language )
                              {
                                  case Language.CSharp :
                                  {
                  
                  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