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