943,862 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3054
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 27th, 2009
0

read file

Expand Post »
Hi,

I am new to c# and I am trying to read a file which is of format:

1 2 3
3 4 5
6 7 8

1 2
3 4

the problem is I am not able to read file in given format, I am not able to tell the streamReader to read separately. Like for example if I ask to read till end of file I can't seperate the format altogether. I have to store first format in different arrays and the other format into different arrays for example:
arrayList A will store 1,3,6
arrayList B will store 2,4,7
arrayList C will store 3,5,8

and 2 other arrays will store
arrayList D will store 1,3
arrayList f will store 2,4

I tried to search it but I didn't got any success till now.
some code that I tried was:
C# Syntax (Toggle Plain Text)
  1. StreamReader myFile = new StreamReader(fileInputTextBox.Text);
  2. //Reads the file line by line
  3. while (!myFile.EndOfStream)
  4. {
  5. string myString = myFile.ReadLine(); //reads file line by line
  6. fillArray(myString);
  7. }
  8. myFile.Close();

This is the code I am using to read file, since I don't know how to make the streamReader read till my format change I am just reading till end of stream.
another method that I use to store it into an array:
C# Syntax (Toggle Plain Text)
  1. public void formArray(string fileString)
  2. {
  3. string filestring=fileString;
  4. string[] tokens = new string[3];
  5. tokens =filestring.Split(' '); //split string with delimiter as space
  6. A.Add(tokens[0]);
  7. B.Add(tokens[1]);
  8. C.Add(tokens[2]);
  9. //don't know how to work for D and F
  10. }

I am really confused can anyone suggest how can I do that.

Thanks!!
Similar Threads
NT.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NT. is offline Offline
14 posts
since Sep 2009
Sep 27th, 2009
0

Re: read file

Is the data structure static in that it will always be in the format exampled?
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 27th, 2009
0

Re: read file

You could check tokens.Length and then act accordingly.
Should also advise you to use the generic List<> type instead of ArrayList.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Sep 27th, 2009
0

Re: read file

This is an odd task. As danny pointed out the generic list is definetly the way to go! You could do something like this:

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace daniweb
  12. {
  13. public partial class frmWeirdHomework : Form
  14. {
  15. public frmWeirdHomework()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private static string[] GetSampleData()
  21. {
  22. List<string> sl = new List<string>();
  23. sl.Add("1 2 3");
  24. sl.Add("3 4 5");
  25. sl.Add("6 7 8");
  26. sl.Add("");
  27. sl.Add("1 2 ");
  28. sl.Add("3 4 ");
  29. return sl.ToArray();
  30. }
  31.  
  32. private static bool IsNumericArray(string[] arr)
  33. {
  34. if ((arr == null) || (arr.Length == 0))
  35. return false;
  36.  
  37. int i;
  38.  
  39. foreach (string s in arr)
  40. {
  41. if (!int.TryParse(s, out i))
  42. return false;
  43. }
  44. return true;
  45. }
  46.  
  47. private void button2_Click(object sender, EventArgs e)
  48. {
  49. //string[] data = File.ReadAllLines(@"C:\yourfile.txt");
  50. string[] data = GetSampleData();
  51. List<List<int>> lstAllItems = new List<List<int>>(); //master list
  52.  
  53. List<List<int>> workingSet = new List<List<int>>(); //holds our working lists until the format changes
  54.  
  55. foreach (string line in data)
  56. {
  57. if (string.IsNullOrEmpty(line))
  58. continue;
  59. string[] sVals = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  60. if (!IsNumericArray(sVals))
  61. continue;
  62.  
  63. int[] iVals = Array.ConvertAll<string, int>(sVals, Convert.ToInt32);
  64. if (workingSet.Count != iVals.Length) //our format changed
  65. {
  66. lstAllItems.AddRange(workingSet.ToArray());
  67. workingSet.Clear();
  68. for (int i1 = 0; i1 < iVals.Length; i1++)
  69. {
  70. workingSet.Add(new List<int>());
  71. }
  72. }
  73. for (int i1 = 0; i1 < iVals.Length; i1++)
  74. {
  75. workingSet[i1].Add(iVals[i1]);
  76. }
  77. }
  78. if (workingSet.Count != 0)
  79. lstAllItems.AddRange(workingSet.ToArray());
  80.  
  81.  
  82. for (int i1 = 0; i1 < lstAllItems.Count; i1++)
  83. {
  84. List<int> lst = lstAllItems[i1];
  85. Console.WriteLine("Array[{0:F0}]: {1}", i1, string.Join(", ", lst.ConvertAll<string>(Convert.ToString).ToArray()));
  86. }
  87. }
  88.  
  89. }
  90. }

text Syntax (Toggle Plain Text)
  1. Array[0]: 1, 3, 6
  2. Array[1]: 2, 4, 7
  3. Array[2]: 3, 5, 8
  4. Array[3]: 1, 3
  5. Array[4]: 2, 4

I'm sure you can figure out how to make 0->A, 1->B, etc
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 27th, 2009
0

Re: read file

Click to Expand / Collapse  Quote originally posted by ddanbe ...
You could check tokens.Length and then act accordingly.
Should also advise you to use the generic List<> type instead of ArrayList.
Thanks a lot for the advice. It worked out. I thought if you do not specify array size while you initialize it gives error but it didn't gave a error. Can you tell me why you asked me to used list instead of arraylist. what's the advantage? It might be silly question but I really wanna know.
NT.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NT. is offline Offline
14 posts
since Sep 2009
Sep 27th, 2009
0

Re: read file

Click to Expand / Collapse  Quote originally posted by sknake ...
This is an odd task. As danny pointed out the generic list is definetly the way to go! You could do something like this:

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace daniweb
  12. {
  13. public partial class frmWeirdHomework : Form
  14. {
  15. public frmWeirdHomework()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private static string[] GetSampleData()
  21. {
  22. List<string> sl = new List<string>();
  23. sl.Add("1 2 3");
  24. sl.Add("3 4 5");
  25. sl.Add("6 7 8");
  26. sl.Add("");
  27. sl.Add("1 2 ");
  28. sl.Add("3 4 ");
  29. return sl.ToArray();
  30. }
  31.  
  32. private static bool IsNumericArray(string[] arr)
  33. {
  34. if ((arr == null) || (arr.Length == 0))
  35. return false;
  36.  
  37. int i;
  38.  
  39. foreach (string s in arr)
  40. {
  41. if (!int.TryParse(s, out i))
  42. return false;
  43. }
  44. return true;
  45. }
  46.  
  47. private void button2_Click(object sender, EventArgs e)
  48. {
  49. //string[] data = File.ReadAllLines(@"C:\yourfile.txt");
  50. string[] data = GetSampleData();
  51. List<List<int>> lstAllItems = new List<List<int>>(); //master list
  52.  
  53. List<List<int>> workingSet = new List<List<int>>(); //holds our working lists until the format changes
  54.  
  55. foreach (string line in data)
  56. {
  57. if (string.IsNullOrEmpty(line))
  58. continue;
  59. string[] sVals = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  60. if (!IsNumericArray(sVals))
  61. continue;
  62.  
  63. int[] iVals = Array.ConvertAll<string, int>(sVals, Convert.ToInt32);
  64. if (workingSet.Count != iVals.Length) //our format changed
  65. {
  66. lstAllItems.AddRange(workingSet.ToArray());
  67. workingSet.Clear();
  68. for (int i1 = 0; i1 < iVals.Length; i1++)
  69. {
  70. workingSet.Add(new List<int>());
  71. }
  72. }
  73. for (int i1 = 0; i1 < iVals.Length; i1++)
  74. {
  75. workingSet[i1].Add(iVals[i1]);
  76. }
  77. }
  78. if (workingSet.Count != 0)
  79. lstAllItems.AddRange(workingSet.ToArray());
  80.  
  81.  
  82. for (int i1 = 0; i1 < lstAllItems.Count; i1++)
  83. {
  84. List<int> lst = lstAllItems[i1];
  85. Console.WriteLine("Array[{0:F0}]: {1}", i1, string.Join(", ", lst.ConvertAll<string>(Convert.ToString).ToArray()));
  86. }
  87. }
  88.  
  89. }
  90. }

text Syntax (Toggle Plain Text)
  1. Array[0]: 1, 3, 6
  2. Array[1]: 2, 4, 7
  3. Array[2]: 3, 5, 8
  4. Array[3]: 1, 3
  5. Array[4]: 2, 4

I'm sure you can figure out how to make 0->A, 1->B, etc
I am really thankful for such a nice example. But I wouldlike to ask the same question to you as well. why we are using list instead of arraylist? Please can you give some brief explanation?
NT.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NT. is offline Offline
14 posts
since Sep 2009
Sep 27th, 2009
2

Re: read file

Hello.
Both ArrayList and List classes are provided to work with dynamic arrays. But, in ArrayList all elements have type of System.Object. And in List you specify the type of elements by yourself. Let's see what it gives to you:

1. Suppose, you have an array of strings.
For ArrayList: while adding new element to array - it will be
cast to an Object type and then added to list. Then to take the
value of it - you'd have to cast it back to String.
c# Syntax (Toggle Plain Text)
  1. ArrayList list = new ArrayList();
  2. list.Add("String element");
  3.  
  4. string element = (string)list[0];
For List: you're just define a List of strings and there's no need
in boxing/unboxing operations (casting to Object type and back).
c# Syntax (Toggle Plain Text)
  1. List<string> otherList = new List<string>();
  2. otherList.Add("String element");
  3. string otherElement = otherList[0];

2. Since the every reference type can be cast to System.Object and every value type can be boxed into an Object, it's possible to have something like this (in ArrayList):
c# Syntax (Toggle Plain Text)
  1. ArrayList list = new ArrayList();
  2. list.Add("String element");
  3. list.Add(2);

The List<T> is strong-typed, so it's one more advantage. E.g. it wouldn't allow you to add Integer in your list of Strings. You will be warned at compilation time that you're trying to do something illegal.
c# Syntax (Toggle Plain Text)
  1. List<string> otherList = new List<string>();
  2. otherList.Add("String element");
  3. otherList.Add(2); // compilation time error

3. And here's a nice post, where performance of List And ArrayList was compared: ArrayList’s vs. generic List for primitive types and 64-bits
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Sep 27th, 2009
0

Re: read file

Click to Expand / Collapse  Quote originally posted by Antenka ...
Hello.
Both ArrayList and List classes are provided to work with dynamic arrays. But, in ArrayList all elements have type of System.Object. And in List you specify the type of elements by yourself. Let's see what it gives to you:

1. Suppose, you have an array of strings.
For ArrayList: while adding new element to array - it will be
cast to an Object type and then added to list. Then to take the
value of it - you'd have to cast it back to String.
c# Syntax (Toggle Plain Text)
  1. ArrayList list = new ArrayList();
  2. list.Add("String element");
  3.  
  4. string element = (string)list[0];
For List: you're just define a List of strings and there's no need
in boxing/unboxing operations (casting to Object type and back).
c# Syntax (Toggle Plain Text)
  1. List<string> otherList = new List<string>();
  2. otherList.Add("String element");
  3. string otherElement = otherList[0];

2. Since the every reference type can be cast to System.Object and every value type can be boxed into an Object, it's possible to have something like this (in ArrayList):
c# Syntax (Toggle Plain Text)
  1. ArrayList list = new ArrayList();
  2. list.Add("String element");
  3. list.Add(2);

The List<T> is strong-typed, so it's one more advantage. E.g. it wouldn't allow you to add Integer in your list of Strings. You will be warned at compilation time that you're trying to do something illegal.
c# Syntax (Toggle Plain Text)
  1. List<string> otherList = new List<string>();
  2. otherList.Add("String element");
  3. otherList.Add(2); // compilation time error

3. And here's a nice post, where performance of List And ArrayList was compared: ArrayList’s vs. generic List for primitive types and 64-bits
Hey, thanks a lot!! I really get a good idea now why not to use arraylist when it is a better idea using list. That was a nice explanation.
NT.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NT. is offline Offline
14 posts
since Sep 2009
Sep 27th, 2009
1

Re: read file

You're welcome
Good luck with learning C#.

Please, mark this thread as solved if the issue is settled.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Sep 27th, 2009
0

Re: read file

>> Antenka
Great explanation

Please mark this thread as solved if you have found an answer to your question and good luck!
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Controls added to panel are moving down when repainted
Next Thread in C# Forum Timeline: map prob





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC