notepad

Reply

Join Date: Nov 2009
Posts: 29
Reputation: sidd. is an unknown quantity at this point 
Solved Threads: 0
sidd. sidd. is offline Offline
Light Poster

notepad

 
0
  #1
Nov 11th, 2009
hi,
i have some values in a notepad (.dat file). i want to store the values one by one in an variable. i dont know how to read the file and store the vaues.
can u help plz...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,284
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 356
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Nearly a Posting Maven
 
1
  #2
Nov 11th, 2009
Hi Sidd. welcome here!
Try this:
  1. using (StreamReader sr = new StreamReader ("myfile.dat"))
  2. {
  3. string s = sr.ReadLine();
  4. //etc.
  5. }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 463
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 56
Diamonddrake's Avatar
Diamonddrake Diamonddrake is online now Online
Posting Pro in Training
 
0
  #3
Nov 11th, 2009
  1. List<string> mylines = new List<string>();
  2. using (StreamReader sr = new StreamReader("myfile.dat"))
  3. {
  4. while (sr.ReadLine() != null)
  5. {
  6. mylines.Add(sr.ReadLine());
  7. }
  8. }

Yeah, That should work. assuming each line is a separate value, otherwise you will need to load it all to memory as a string and split it to an array using the delimiter. Post back if you need more help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 30
Reputation: sanch01r is an unknown quantity at this point 
Solved Threads: 3
sanch01r sanch01r is offline Offline
Light Poster
 
0
  #4
Nov 15th, 2009
Just in case, here is a method to read from a CSV or delimiter. It returns data to its caller.

  1.  
  2. static void Main(string[] args)
  3. {
  4.  
  5. List<string> columns;
  6. List<Dictionary<string, string>> myData = GetData(out columns);
  7.  
  8. foreach (string column in columns)
  9. {
  10. Console.Write("{0,-20}", column);
  11. }
  12. Console.WriteLine();
  13.  
  14. foreach (Dictionary<string, string> row in myData)
  15. {
  16. foreach (string column in columns)
  17. {
  18. Console.Write("{0, -20}", row[column]);
  19. }
  20. Console.WriteLine();
  21. }
  22. Console.ReadKey();
  23.  
  24.  
  25. }
  26. public static List<Dictionary<string, string>> GetData(out List<string> columns)
  27. {
  28. string strLine;
  29. string[] strArray;
  30. char[] charArray = new char[] { ',' };
  31. List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
  32. columns = new List<string>();
  33.  
  34.  
  35. try
  36. {
  37.  
  38. StreamReader sr = new StreamReader(new FileStream("C:\\TECHDATA.txt", FileMode.Open));
  39.  
  40. strLine = sr.ReadLine();
  41. strArray = strLine.Split(charArray);
  42.  
  43. for (int x = 0; x <= strArray.GetUpperBound(0); x++)
  44. {
  45. columns.Add(strArray[x]);
  46. }
  47.  
  48. strLine = sr.ReadLine();
  49.  
  50. while (strLine != null)
  51. {
  52. strArray = strLine.Split(charArray);
  53. Dictionary<string, string> dataRow = new Dictionary<string, string>();
  54.  
  55. for (int x = 0; x <= strArray.GetUpperBound(0); x++)
  56. {
  57. dataRow.Add(columns[x], strArray[x]);
  58. }
  59. data.Add(dataRow);
  60. strLine = sr.ReadLine();
  61. }
  62. sr.Dispose();
  63. return data;
  64. }
  65. catch (IOException ex)
  66. {
  67. Console.WriteLine(ex.Message);
  68. }
  69.  
  70. return data;
  71.  
  72. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum


Views: 566 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC