Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to store some data on text file which will be used in the program later. Data contains one sting data and one numeric data on each record. It needs to store only a single record. Can u tell me >> How can I store different data from different text boxes and read and input in different variables.
Posted

1 solution

It's not trivial, you have to think about your data first. If your string can contain any character, then you need to put the number first, followed by a delimiter to show where it ends, then the string value:
C#
string writeMe = string.Format("{0};{1}", numericValue, stringValue);
will genare a string to write to the file safely.
You can then read it back, and convert it back to usable values again:
C#
string input = "12345.67;This is the text part";
int sep = input.IndexOf(';');
double numericValue = double.Parse(input.Substring(0, sep));
string strVal = input.Substring(sep + 1);
 
Share this answer
 
Comments
Darpan Dahal 7-Apr-13 7:58am    
Thanks! and if both are string then what changes should be made?
OriginalGriff 7-Apr-13 8:20am    
That gets a lot more complex, because either you have to use a separator which cannot be in either string (generally difficult to find), or translate your strings so that the chosen separator doesn't appear.
But if you are doing that, it would probably be a good idea to stop looking at a "straight" text file, and look at using some sort of organised storage instead. CSV (which just stands for "Comma Separated Values" and does the translating I mentioned for you), or XML are good simple to use formats, or even a small database such as SqlCE or SqLite would be compact and flexible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900