reading text file as an array by using streamreader
-
Text File in Notepad++
1. Name
2. Institute
3. .......................................................................
4. x : y
5. ......................................................................
6. 9 7
7. .......................................................................
8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
9. .......................................................................
10. 4.0 5.0 3.56 4.78 5.098 2
11. 5.0 6.0 4.67 5.87 6.387 3
12. 3.0 4.0 6.49 3.95 5.209 4
13. 4.0 7.0 2.01 6.98 2.039 9
14. 9.0 2.0 5.02 2.94 7.209 2
15. 8.0 2.0 4.02 7.30 8.202 6
16. 5.0 9.0 7.03 3.06 2.049 3
17. 0.5 0.4 0.34 0.34 0.534 9
18. 0.7 0.2 0.53 0.74 0.293 3
19. ........................................................................
20.
21.Code in C#
using System;
using System.IO;
using System.Globalization;namespace main
{
public class project
{
public int x, y;
public matrix MandW, Time, VandA, Distance;
public string cmt, file, dash;
public project()
{
string filename= "C://user//abc.txt";
Read(filename);
}
public void Read(String pfile)
{
file=pfile;
CultureInfo _ci = CultureInfo.InvariantCulture;
using (StreamReader _sr = new StreamReader(pfile))
{
string[] values;
cmt = _sr.ReadLine(); /* reading keyword Name */
cmt = _sr.ReadLine(); /* reading keyword institute */
dash = _sr.ReadLine(); /* reading ............*/
cmt = _sr.ReadLine(); /* reading x : y */
dash = _sr.ReadLine(); /*reading ..............*/
values = IO.split_string(_sr.ReadLine());
x = Convert.ToInt32(values[0]);
y = Convert.ToInt32(values[1]);
dash = _sr.ReadLine(); /*reading ...............*/
cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
dash = _sr.ReadLine();
MandW = new matrix(x,2); /* Mass and Weight Matrix*/
for (int i= 0, iI am having trouble constructing these matrices..
Thank you in advance. -
Text File in Notepad++
1. Name
2. Institute
3. .......................................................................
4. x : y
5. ......................................................................
6. 9 7
7. .......................................................................
8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
9. .......................................................................
10. 4.0 5.0 3.56 4.78 5.098 2
11. 5.0 6.0 4.67 5.87 6.387 3
12. 3.0 4.0 6.49 3.95 5.209 4
13. 4.0 7.0 2.01 6.98 2.039 9
14. 9.0 2.0 5.02 2.94 7.209 2
15. 8.0 2.0 4.02 7.30 8.202 6
16. 5.0 9.0 7.03 3.06 2.049 3
17. 0.5 0.4 0.34 0.34 0.534 9
18. 0.7 0.2 0.53 0.74 0.293 3
19. ........................................................................
20.
21.Code in C#
using System;
using System.IO;
using System.Globalization;namespace main
{
public class project
{
public int x, y;
public matrix MandW, Time, VandA, Distance;
public string cmt, file, dash;
public project()
{
string filename= "C://user//abc.txt";
Read(filename);
}
public void Read(String pfile)
{
file=pfile;
CultureInfo _ci = CultureInfo.InvariantCulture;
using (StreamReader _sr = new StreamReader(pfile))
{
string[] values;
cmt = _sr.ReadLine(); /* reading keyword Name */
cmt = _sr.ReadLine(); /* reading keyword institute */
dash = _sr.ReadLine(); /* reading ............*/
cmt = _sr.ReadLine(); /* reading x : y */
dash = _sr.ReadLine(); /*reading ..............*/
values = IO.split_string(_sr.ReadLine());
x = Convert.ToInt32(values[0]);
y = Convert.ToInt32(values[1]);
dash = _sr.ReadLine(); /*reading ...............*/
cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
dash = _sr.ReadLine();
MandW = new matrix(x,2); /* Mass and Weight Matrix*/
for (int i= 0, iI am having trouble constructing these matrices..
Thank you in advance.Member 14571520 wrote:
I am having trouble constructing these matrices..
What trouble? What does it do that you didn't expect, or not do that you did? Any error messages? What have you tried to fix it?
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Text File in Notepad++
1. Name
2. Institute
3. .......................................................................
4. x : y
5. ......................................................................
6. 9 7
7. .......................................................................
8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
9. .......................................................................
10. 4.0 5.0 3.56 4.78 5.098 2
11. 5.0 6.0 4.67 5.87 6.387 3
12. 3.0 4.0 6.49 3.95 5.209 4
13. 4.0 7.0 2.01 6.98 2.039 9
14. 9.0 2.0 5.02 2.94 7.209 2
15. 8.0 2.0 4.02 7.30 8.202 6
16. 5.0 9.0 7.03 3.06 2.049 3
17. 0.5 0.4 0.34 0.34 0.534 9
18. 0.7 0.2 0.53 0.74 0.293 3
19. ........................................................................
20.
21.Code in C#
using System;
using System.IO;
using System.Globalization;namespace main
{
public class project
{
public int x, y;
public matrix MandW, Time, VandA, Distance;
public string cmt, file, dash;
public project()
{
string filename= "C://user//abc.txt";
Read(filename);
}
public void Read(String pfile)
{
file=pfile;
CultureInfo _ci = CultureInfo.InvariantCulture;
using (StreamReader _sr = new StreamReader(pfile))
{
string[] values;
cmt = _sr.ReadLine(); /* reading keyword Name */
cmt = _sr.ReadLine(); /* reading keyword institute */
dash = _sr.ReadLine(); /* reading ............*/
cmt = _sr.ReadLine(); /* reading x : y */
dash = _sr.ReadLine(); /*reading ..............*/
values = IO.split_string(_sr.ReadLine());
x = Convert.ToInt32(values[0]);
y = Convert.ToInt32(values[1]);
dash = _sr.ReadLine(); /*reading ...............*/
cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
dash = _sr.ReadLine();
MandW = new matrix(x,2); /* Mass and Weight Matrix*/
for (int i= 0, iI am having trouble constructing these matrices..
Thank you in advance.Your main problem seems to be that you do no checks on the data you are reading. You should check the content of each line to ensure it contains the elements you expect. You should also check the result of your calls to
IO.split_string
; and why are you not usingstring.split()
? And do not useConvert
to convert strings to numbers, use theTryParse()
method for each type; that way you will not be caught out by bad data. And finally, if you look at your data the lines containing the mass and weight values have a line number at the beginning which will confuse the converter:10. 4.0 5.0 3.56 4.78 5.098 2
. -
Text File in Notepad++
1. Name
2. Institute
3. .......................................................................
4. x : y
5. ......................................................................
6. 9 7
7. .......................................................................
8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
9. .......................................................................
10. 4.0 5.0 3.56 4.78 5.098 2
11. 5.0 6.0 4.67 5.87 6.387 3
12. 3.0 4.0 6.49 3.95 5.209 4
13. 4.0 7.0 2.01 6.98 2.039 9
14. 9.0 2.0 5.02 2.94 7.209 2
15. 8.0 2.0 4.02 7.30 8.202 6
16. 5.0 9.0 7.03 3.06 2.049 3
17. 0.5 0.4 0.34 0.34 0.534 9
18. 0.7 0.2 0.53 0.74 0.293 3
19. ........................................................................
20.
21.Code in C#
using System;
using System.IO;
using System.Globalization;namespace main
{
public class project
{
public int x, y;
public matrix MandW, Time, VandA, Distance;
public string cmt, file, dash;
public project()
{
string filename= "C://user//abc.txt";
Read(filename);
}
public void Read(String pfile)
{
file=pfile;
CultureInfo _ci = CultureInfo.InvariantCulture;
using (StreamReader _sr = new StreamReader(pfile))
{
string[] values;
cmt = _sr.ReadLine(); /* reading keyword Name */
cmt = _sr.ReadLine(); /* reading keyword institute */
dash = _sr.ReadLine(); /* reading ............*/
cmt = _sr.ReadLine(); /* reading x : y */
dash = _sr.ReadLine(); /*reading ..............*/
values = IO.split_string(_sr.ReadLine());
x = Convert.ToInt32(values[0]);
y = Convert.ToInt32(values[1]);
dash = _sr.ReadLine(); /*reading ...............*/
cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
dash = _sr.ReadLine();
MandW = new matrix(x,2); /* Mass and Weight Matrix*/
for (int i= 0, iI am having trouble constructing these matrices..
Thank you in advance.I haven't heard about
IO.split_string()
method and i can't find it in MSDN documentation... Can you share direct link into it? -
Text File in Notepad++
1. Name
2. Institute
3. .......................................................................
4. x : y
5. ......................................................................
6. 9 7
7. .......................................................................
8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
9. .......................................................................
10. 4.0 5.0 3.56 4.78 5.098 2
11. 5.0 6.0 4.67 5.87 6.387 3
12. 3.0 4.0 6.49 3.95 5.209 4
13. 4.0 7.0 2.01 6.98 2.039 9
14. 9.0 2.0 5.02 2.94 7.209 2
15. 8.0 2.0 4.02 7.30 8.202 6
16. 5.0 9.0 7.03 3.06 2.049 3
17. 0.5 0.4 0.34 0.34 0.534 9
18. 0.7 0.2 0.53 0.74 0.293 3
19. ........................................................................
20.
21.Code in C#
using System;
using System.IO;
using System.Globalization;namespace main
{
public class project
{
public int x, y;
public matrix MandW, Time, VandA, Distance;
public string cmt, file, dash;
public project()
{
string filename= "C://user//abc.txt";
Read(filename);
}
public void Read(String pfile)
{
file=pfile;
CultureInfo _ci = CultureInfo.InvariantCulture;
using (StreamReader _sr = new StreamReader(pfile))
{
string[] values;
cmt = _sr.ReadLine(); /* reading keyword Name */
cmt = _sr.ReadLine(); /* reading keyword institute */
dash = _sr.ReadLine(); /* reading ............*/
cmt = _sr.ReadLine(); /* reading x : y */
dash = _sr.ReadLine(); /*reading ..............*/
values = IO.split_string(_sr.ReadLine());
x = Convert.ToInt32(values[0]);
y = Convert.ToInt32(values[1]);
dash = _sr.ReadLine(); /*reading ...............*/
cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
dash = _sr.ReadLine();
MandW = new matrix(x,2); /* Mass and Weight Matrix*/
for (int i= 0, iI am having trouble constructing these matrices..
Thank you in advance.If the file content has always got the same structure, you can to grab data into specific class. For example:
public class MyData
{
private System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
public string Name {get; set;}
public string Institute {get; set;}
public int X {get; set;}
public int Y {get; set;}
public string[] Headers {get; set;}
public double[][] DataMatrix {get; set;}
public MyData(string sFileName)
{
if(!File.Exists(sFileName))
throw new FileNotFoundException($"File '{Path.GetFileName(sFileName)}' does not exist!");string\[\] lines = File.ReadAllLines(sFileName); Name = lines\[0\]; Institute = lines\[1\]; X = Convert.ToInt32(lines\[5\].Split(' ')\[0\]); Y = Convert.ToInt32(lines\[5\].Split(' ')\[1\]); Headers = lines\[7\].Split(new string\[\]{" : "}, StringSplitOptions.RemoveEmptyEntries); DataMatrix = lines.Skip(9) .TakeWhile(l=>l.Length>0 && !l.StartsWith(".")) .Select(l=>l.Split(' ').Select(d=>Double.Parse(d, ci)).ToArray()) .ToArray(); }
}
Then usage will be very easy:
string filename= "C://user//abc.txt";
MyData md = new MyData(filename);
Console.WriteLine(md.Name);
Console.WriteLine(md.Institute);
Console.WriteLine("X = {0}, Y = {1}", md.X, md.Y);
double[] allMasses = md.DataMatrix.Select(m=>m[0]).ToArray();
foreach(double d in allMasses)
Console.WriteLine(d);
//on the same manner you can get Weight, Time, Velocity, Acceleration and DistanceGood luck!
-
Text File in Notepad++
1. Name
2. Institute
3. .......................................................................
4. x : y
5. ......................................................................
6. 9 7
7. .......................................................................
8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
9. .......................................................................
10. 4.0 5.0 3.56 4.78 5.098 2
11. 5.0 6.0 4.67 5.87 6.387 3
12. 3.0 4.0 6.49 3.95 5.209 4
13. 4.0 7.0 2.01 6.98 2.039 9
14. 9.0 2.0 5.02 2.94 7.209 2
15. 8.0 2.0 4.02 7.30 8.202 6
16. 5.0 9.0 7.03 3.06 2.049 3
17. 0.5 0.4 0.34 0.34 0.534 9
18. 0.7 0.2 0.53 0.74 0.293 3
19. ........................................................................
20.
21.Code in C#
using System;
using System.IO;
using System.Globalization;namespace main
{
public class project
{
public int x, y;
public matrix MandW, Time, VandA, Distance;
public string cmt, file, dash;
public project()
{
string filename= "C://user//abc.txt";
Read(filename);
}
public void Read(String pfile)
{
file=pfile;
CultureInfo _ci = CultureInfo.InvariantCulture;
using (StreamReader _sr = new StreamReader(pfile))
{
string[] values;
cmt = _sr.ReadLine(); /* reading keyword Name */
cmt = _sr.ReadLine(); /* reading keyword institute */
dash = _sr.ReadLine(); /* reading ............*/
cmt = _sr.ReadLine(); /* reading x : y */
dash = _sr.ReadLine(); /*reading ..............*/
values = IO.split_string(_sr.ReadLine());
x = Convert.ToInt32(values[0]);
y = Convert.ToInt32(values[1]);
dash = _sr.ReadLine(); /*reading ...............*/
cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
dash = _sr.ReadLine();
MandW = new matrix(x,2); /* Mass and Weight Matrix*/
for (int i= 0, iI am having trouble constructing these matrices..
Thank you in advance.imho, the solution you really want requires you ... or someone else ... writing the code to serialize the data into XML where it can then be deserialized into an instance of a a Class, or added, by binding, to some grid UI. If that's not possible, I am afraid you are stuck with the tedious task of implementing a line-by-line parser with error-checking per line, as Richard suggested.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
Text File in Notepad++
1. Name
2. Institute
3. .......................................................................
4. x : y
5. ......................................................................
6. 9 7
7. .......................................................................
8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
9. .......................................................................
10. 4.0 5.0 3.56 4.78 5.098 2
11. 5.0 6.0 4.67 5.87 6.387 3
12. 3.0 4.0 6.49 3.95 5.209 4
13. 4.0 7.0 2.01 6.98 2.039 9
14. 9.0 2.0 5.02 2.94 7.209 2
15. 8.0 2.0 4.02 7.30 8.202 6
16. 5.0 9.0 7.03 3.06 2.049 3
17. 0.5 0.4 0.34 0.34 0.534 9
18. 0.7 0.2 0.53 0.74 0.293 3
19. ........................................................................
20.
21.Code in C#
using System;
using System.IO;
using System.Globalization;namespace main
{
public class project
{
public int x, y;
public matrix MandW, Time, VandA, Distance;
public string cmt, file, dash;
public project()
{
string filename= "C://user//abc.txt";
Read(filename);
}
public void Read(String pfile)
{
file=pfile;
CultureInfo _ci = CultureInfo.InvariantCulture;
using (StreamReader _sr = new StreamReader(pfile))
{
string[] values;
cmt = _sr.ReadLine(); /* reading keyword Name */
cmt = _sr.ReadLine(); /* reading keyword institute */
dash = _sr.ReadLine(); /* reading ............*/
cmt = _sr.ReadLine(); /* reading x : y */
dash = _sr.ReadLine(); /*reading ..............*/
values = IO.split_string(_sr.ReadLine());
x = Convert.ToInt32(values[0]);
y = Convert.ToInt32(values[1]);
dash = _sr.ReadLine(); /*reading ...............*/
cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
dash = _sr.ReadLine();
MandW = new matrix(x,2); /* Mass and Weight Matrix*/
for (int i= 0, iI am having trouble constructing these matrices..
Thank you in advance. -
Text File in Notepad++
1. Name
2. Institute
3. .......................................................................
4. x : y
5. ......................................................................
6. 9 7
7. .......................................................................
8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
9. .......................................................................
10. 4.0 5.0 3.56 4.78 5.098 2
11. 5.0 6.0 4.67 5.87 6.387 3
12. 3.0 4.0 6.49 3.95 5.209 4
13. 4.0 7.0 2.01 6.98 2.039 9
14. 9.0 2.0 5.02 2.94 7.209 2
15. 8.0 2.0 4.02 7.30 8.202 6
16. 5.0 9.0 7.03 3.06 2.049 3
17. 0.5 0.4 0.34 0.34 0.534 9
18. 0.7 0.2 0.53 0.74 0.293 3
19. ........................................................................
20.
21.Code in C#
using System;
using System.IO;
using System.Globalization;namespace main
{
public class project
{
public int x, y;
public matrix MandW, Time, VandA, Distance;
public string cmt, file, dash;
public project()
{
string filename= "C://user//abc.txt";
Read(filename);
}
public void Read(String pfile)
{
file=pfile;
CultureInfo _ci = CultureInfo.InvariantCulture;
using (StreamReader _sr = new StreamReader(pfile))
{
string[] values;
cmt = _sr.ReadLine(); /* reading keyword Name */
cmt = _sr.ReadLine(); /* reading keyword institute */
dash = _sr.ReadLine(); /* reading ............*/
cmt = _sr.ReadLine(); /* reading x : y */
dash = _sr.ReadLine(); /*reading ..............*/
values = IO.split_string(_sr.ReadLine());
x = Convert.ToInt32(values[0]);
y = Convert.ToInt32(values[1]);
dash = _sr.ReadLine(); /*reading ...............*/
cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
dash = _sr.ReadLine();
MandW = new matrix(x,2); /* Mass and Weight Matrix*/
for (int i= 0, iI am having trouble constructing these matrices..
Thank you in advance.Member 14571520 wrote:
MandW = new matrix(x,2); /* Mass and Weight Matrix*/
Unless
matrix
is an actual class name, I think you wanted something likeMandW = new double[x,2]; /* Mass and Weight Matrix*/
Class names should by UpperCamelCase. Also I'm not sure if you should call Read() in a constructor... let external code decide when does it want to do this