read file

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 11
Reputation: NT. is an unknown quantity at this point 
Solved Threads: 0
NT. NT. is offline Offline
Newbie Poster

read file

 
0
  #1
Sep 27th, 2009
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:
  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:
  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!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 964
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 206
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: read file

 
0
  #2
Sep 27th, 2009
Is the data structure static in that it will always be in the format exampled?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,031
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: 302
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

Re: read file

 
0
  #3
Sep 27th, 2009
You could check tokens.Length and then act accordingly.
Should also advise you to use the generic List<> type instead of ArrayList.
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: Feb 2009
Posts: 3,410
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 614
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: read file

 
0
  #4
Sep 27th, 2009
This is an odd task. As danny pointed out the generic list is definetly the way to go! You could do something like this:

  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. }

  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
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: NT. is an unknown quantity at this point 
Solved Threads: 0
NT. NT. is offline Offline
Newbie Poster

Re: read file

 
0
  #5
Sep 27th, 2009
Originally Posted by ddanbe View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: NT. is an unknown quantity at this point 
Solved Threads: 0
NT. NT. is offline Offline
Newbie Poster

Re: read file

 
0
  #6
Sep 27th, 2009
Originally Posted by sknake View Post
This is an odd task. As danny pointed out the generic list is definetly the way to go! You could do something like this:

  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. }

  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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 254
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: read file

 
2
  #7
Sep 27th, 2009
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.
  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).
  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):
  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.
  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
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: NT. is an unknown quantity at this point 
Solved Threads: 0
NT. NT. is offline Offline
Newbie Poster

Re: read file

 
0
  #8
Sep 27th, 2009
Originally Posted by Antenka View Post
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.
  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).
  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):
  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 254
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: read file

 
1
  #9
Sep 27th, 2009
You're welcome
Good luck with learning C#.

Please, mark this thread as solved if the issue is settled.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,410
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 614
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: read file

 
0
  #10
Sep 27th, 2009
>> Antenka
Great explanation

Please mark this thread as solved if you have found an answer to your question and good luck!
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Tags
array, file

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for array, file
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC