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. Using textfile as a storage system for numerical data...

Using textfile as a storage system for numerical data...

Scheduled Pinned Locked Moved C#
cryptographyhelptutorial
9 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.
  • S Offline
    S Offline
    sebogawa
    wrote on last edited by
    #1

    Hi, Im trying to find out the simplest way to store variable values in a file (text or otherwise), as well as being able to read specific areas of the file in order to retrive the values and implement them into a windows form control like a textbox or something. The StreamReader and StreamWriter class works but I dont know how to read specific lines like line number 4 or so. Here is my code:

    StreamReader sr = new StreamReader("configFile.txt", System.Text.Encoding.Default);
    string s = null;

    		while ((s = sr.ReadLine()) != null)
    		{
    			if (s.IndexOf("Name:") != -1)
    			{
    				 
    				// textBox1.text = s;
    				break;
    			}
    		}
    
    		sr.Close();
    

    The issue is that I cant select what to search for by line number. All i need is a simple way to add textbox values to a text file where each texbox has its own line in the text file, and I can call up the text file at a certain line of the file and read the contents/make it the textbox text. The data is numerical. I came across hash tables but I do not know how to implement them/save them to a file.

    R L 2 Replies Last reply
    0
    • S sebogawa

      Hi, Im trying to find out the simplest way to store variable values in a file (text or otherwise), as well as being able to read specific areas of the file in order to retrive the values and implement them into a windows form control like a textbox or something. The StreamReader and StreamWriter class works but I dont know how to read specific lines like line number 4 or so. Here is my code:

      StreamReader sr = new StreamReader("configFile.txt", System.Text.Encoding.Default);
      string s = null;

      		while ((s = sr.ReadLine()) != null)
      		{
      			if (s.IndexOf("Name:") != -1)
      			{
      				 
      				// textBox1.text = s;
      				break;
      			}
      		}
      
      		sr.Close();
      

      The issue is that I cant select what to search for by line number. All i need is a simple way to add textbox values to a text file where each texbox has its own line in the text file, and I can call up the text file at a certain line of the file and read the contents/make it the textbox text. The data is numerical. I came across hash tables but I do not know how to implement them/save them to a file.

      R Offline
      R Offline
      RCoate
      wrote on last edited by
      #2

      One approach is to use a xml file. Lots of examples on the web of how to do this. Try looking at this[^]

      S 1 Reply Last reply
      0
      • R RCoate

        One approach is to use a xml file. Lots of examples on the web of how to do this. Try looking at this[^]

        S Offline
        S Offline
        sebogawa
        wrote on last edited by
        #3

        Awesome advice! Thnks! Here is a website for anyone who is curious as to how to work with XML from the gorund up. Very comprehensive tutorials. http://www.c-sharpcorner.com/uploadfile/mahesh/readwritexmltutmellli2111282005041517am/readwritexmltutmellli21.aspx

        1 Reply Last reply
        0
        • S sebogawa

          Hi, Im trying to find out the simplest way to store variable values in a file (text or otherwise), as well as being able to read specific areas of the file in order to retrive the values and implement them into a windows form control like a textbox or something. The StreamReader and StreamWriter class works but I dont know how to read specific lines like line number 4 or so. Here is my code:

          StreamReader sr = new StreamReader("configFile.txt", System.Text.Encoding.Default);
          string s = null;

          		while ((s = sr.ReadLine()) != null)
          		{
          			if (s.IndexOf("Name:") != -1)
          			{
          				 
          				// textBox1.text = s;
          				break;
          			}
          		}
          
          		sr.Close();
          

          The issue is that I cant select what to search for by line number. All i need is a simple way to add textbox values to a text file where each texbox has its own line in the text file, and I can call up the text file at a certain line of the file and read the contents/make it the textbox text. The data is numerical. I came across hash tables but I do not know how to implement them/save them to a file.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          the simplest solution probably goes like so: - keep key,value pairs in the file, one per line, all keys unique, key and value separated by some symbol (= seems the obvious choice); - read all lines with File.ReadAllLines - inside a foreach loop, split the line using string.Split('=',2), use the first part as a key, the second as a value, and stuff that into a Dictionary Done. Less than 10 lines of code. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


          S 1 Reply Last reply
          0
          • L Luc Pattyn

            the simplest solution probably goes like so: - keep key,value pairs in the file, one per line, all keys unique, key and value separated by some symbol (= seems the obvious choice); - read all lines with File.ReadAllLines - inside a foreach loop, split the line using string.Split('=',2), use the first part as a key, the second as a value, and stuff that into a Dictionary Done. Less than 10 lines of code. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


            S Offline
            S Offline
            sebogawa
            wrote on last edited by
            #5

            Possibly dumb question but how do I use the key you stated? I know its an identifier for the value string but do i just use my code to search for it? Like if (s.IndexOf("fn:") != -1) ? or what?

            L 1 Reply Last reply
            0
            • S sebogawa

              Possibly dumb question but how do I use the key you stated? I know its an identifier for the value string but do i just use my code to search for it? Like if (s.IndexOf("fn:") != -1) ? or what?

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              You create the dictionary like so (omitting error handling):

              Dictionary<string, string> dict=new Dictionary<string, string>();
              foreach(string s in File.ReadAllLines(path)) {
              string parts[]=string.Split('=', 2);
              dict.Remove(parts[0]); // remove if it already exists
              dict.Add(parts[0], parts[1]);
              }

              You look up a key's value by using the key as an index to your dictionary, like so:

              string name=dict\["Name"\];
              

              You enumerate all key-values (maybe not in original order!) like so:

              foreach (KeyValuePair<string, string> kvp in dict) {
              log(kvp.Key+"="+kvp.Value);
              }

              You could also prepopulate the dictionary with default values for some keys:

              dict.Add("Name", "Jef");
              dict.Add("Occupation", "Software Engineer");

              :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


              S 1 Reply Last reply
              0
              • L Luc Pattyn

                You create the dictionary like so (omitting error handling):

                Dictionary<string, string> dict=new Dictionary<string, string>();
                foreach(string s in File.ReadAllLines(path)) {
                string parts[]=string.Split('=', 2);
                dict.Remove(parts[0]); // remove if it already exists
                dict.Add(parts[0], parts[1]);
                }

                You look up a key's value by using the key as an index to your dictionary, like so:

                string name=dict\["Name"\];
                

                You enumerate all key-values (maybe not in original order!) like so:

                foreach (KeyValuePair<string, string> kvp in dict) {
                log(kvp.Key+"="+kvp.Value);
                }

                You could also prepopulate the dictionary with default values for some keys:

                dict.Add("Name", "Jef");
                dict.Add("Occupation", "Software Engineer");

                :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                S Offline
                S Offline
                sebogawa
                wrote on last edited by
                #7

                thanks a heap!

                S 1 Reply Last reply
                0
                • S sebogawa

                  thanks a heap!

                  S Offline
                  S Offline
                  sebogawa
                  wrote on last edited by
                  #8

                  Is the file physically saved anywhere in order for me to acess it later? if so, how do I bring up a dictionary object from a file?

                  L 1 Reply Last reply
                  0
                  • S sebogawa

                    Is the file physically saved anywhere in order for me to acess it later? if so, how do I bring up a dictionary object from a file?

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Hi, you could save settings to disk by creating a loop, similar to the foreach I have shown; and later reload just like you load the first time. PS: replying to yourself, I did not get a notification and saw your message only by accident. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                    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