pessi

dear all

I have a text file with data like
0.923 0.710 0.534 0.786 1.237 1.309 0.977 0.704 0.727
1.350 1.866 2.369 2.632 2.452 1.852 1.235 0.981 0.923
0.570 0.406 0.271 0.443 0.785 0.869 0.649 0.458 0.498
1.396 2.122 2.590 2.569 2.150 1.538 0.978 0.681 0.570

values are seperated with spaces and lines separated by newline.
How to read these values I open the text file using Streamreader
and when I use the ReadLine property it will read the full line but I want to read the
single values.

Thanks in advance,
prasad.




Re: .NET Base Class Library reading a text file.

TilakGopi

Hi,

go in this way

[codelanguage="csharp"]

static void < xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Main()< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

        {           

            string[] strLines = File.ReadAllLines(@"c:\1.txt");

            string[][] strFields = new string[strLines.Length][];

 

            for (int index = 0; index < strLines.Length; index++)

            {                

 

                string[] strTempArray = strLines[index].Split(new char[] {' '});

               

                //code to remove additional spaces between numbers

 

                //Holds the actual numbers count in the line

                int nActualCount = 0;

 

                for (int idx = 0; idx < strTempArray.Length; idx++)

                    if (strTempArray[idx].Trim().Length != 0)

                        nActualCount++;

 

                strFields[index] = new string[nActualCount];

 

                nActualCount = 0;

 

                for (int idx = 0; idx < strTempArray.Length; idx++)

                    if (strTempArray[idx].Trim().Length != 0)

                        strFields[index][nActualCount++] = strTempArray[idx];

 

                for (int idx = 0; idx < strFields[index].Length; idx++)

                {

                    Console.Write(strFields[index][idx] + " ");

                }

                Console.WriteLine();

            }

           

        }

 

[/code]

 

Feel free to communicate for further issues.I hope u r done.

Thanx,

Ch.T.Gopi Kumar.






Re: .NET Base Class Library reading a text file.

pessi

Thank you for your answer. As I am a starter, I am finding it difficult to unserstand the logic...

can you show me the logic to split a string into individual words

say I have a string

mystring = "my name is pessi and I am from India";

assume that the words may have any number of random spaces between them.

now can you please explain me the logic Thank you very much Gopi.

prasad..




Re: .NET Base Class Library reading a text file.

TilakGopi

sure,

split the my string considering the space as delimeter.

Now u'll get an array of strings consisting

 strTokens {Dimensions:[0x00000018]} string[]


  [0x00000000] "my" string
  [0x00000001] "name" string
  [0x00000002] "is" string
  [0x00000003] "pessi" string
  [0x00000004] "" string
  [0x00000005] "" string
  [0x00000006] "" string
  [0x00000007] "" string
  [0x00000008] "and" string
  [0x00000009] "I" string
  [0x0000000a] "am" string
  [0x0000000b] "" string
  [0x0000000c] "" string
  [0x0000000d] "" string
  [0x0000000e] "" string
  [0x0000000f] "" string
  [0x00000010] "" string
  [0x00000011] "" string
  [0x00000012] "from" string
  [0x00000013] "" string
  [0x00000014] "" string
  [0x00000015] "" string
  [0x00000016] "" string
  [0x00000017] "India" string

Now what i'm doing is counting the strings which are not blnaks by checking each elment in the strTokens[] array

(Trim the string ,if it's length is zero - it's blank - that's the logic here).

Now, i will have the actual count.

Similarly i'm copying the elements ,which are not blank to the target array.

Now target array contains only the non blnak elements.

Debug my code , so that u'll get know what's happening.

Hope u r clear now.Not No probls, post ur queries again.

 

Thanx,

Ch.T.Gopi Kumar.






Re: .NET Base Class Library reading a text file.

AQ Arif

Hi Pessi,

You can use RegularExpression for that. Here is the code.

string sample = "Parse this Text if you can ";

Regex reg = new Regex(@"\s+");

foreach(string temp in reg.Split(sample))

{

Console.WriteLine(temp);

}

Best regards,

Arif





Re: .NET Base Class Library reading a text file.

TilakGopi

Yes pessi,

 it seems 'Arif' logic is simpler than that of mine in skipping the blanks.

I'm not good on Regular Expressions concept ,that's why i couldn't make it the best.


Anyways good logic Arif.

 

Best Regards,

Ch.T.Gopi Kumar.






Re: .NET Base Class Library reading a text file.

AQ Arif

Hi Gopi,

You are welcome Gopi.

Kind regards,

Arif





Re: .NET Base Class Library reading a text file.

pessi

You are a gem! Thank you so much!





Re: .NET Base Class Library reading a text file.

pessi

BTW Arif, can you tell you what @"\s+" meant

prasad.




Re: .NET Base Class Library reading a text file.

pessi

never mind. I looked for the answer and found it.

prasad.




Re: .NET Base Class Library reading a text file.

AQ Arif

Hi,

In regular expressions, Whitespaces are identified as "\s+".

Kind regards,

Arif