Solved

Reading text from a file

Posted on 2009-07-01
4
201 Views
Last Modified: 2012-05-07
I need to be able to read text from a file, and save it somehow to a grid like excell, the format inside the file is as follows

"ZERNIKE"
"SCALE:  2.56245844477293E-03"
28
0
7.26319504749052E-04
-1.28435703005351E-04
-4.70736639230834E-04
-2.11696194741656E-04
-6.94188170158255E-06
1.21760327335195E-05
-1.16471189175351E-04
1.892881053328E-04
2.43394089421797E-05
-4.27588262279435E-06
3.96428568852939E-05
1.21416458423661E-05
-5.07598197431613E-06
2.42835581669891E-05
1.84678127720032E-05
-1.6793619674676E-05
-5.20898049006682E-06
-1.76892518358479E-05
-7.00485451263667E-06
1.38926890110059E-05
-1.61087650649244E-05
1.32222083318142E-05
2.28048832601142E-05
4.92909574194228E-05
-3.9719165605381E-05
-3.30874305806735E-05
4.36658825006991E-05
-1
0
-1
-5.05906053272166E-03
-3.51320393781458E-03
6.18699368903292E-04
4.7028011256734E-04
1003597

Each line is needed in a different cell. moving to the right, essentially in the same row of a multidemensional array.

Many of these files will be pulled in sequence.
 
If needed i can split this up into different questions, i just do not know how to do this.
If i am unclear, Please ask questions
0
Comment
Question by:cotj73
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
Solar_Flare earned 500 total points
ID: 24760462
if you have the filename then you can use a streamreader

string filename = "c:\example.txt";
 

        //METHOD 1, read the file line by line

        System.IO.StreamReader rdr = new System.IO.StreamReader(filename);

        while (!rdr.EndOfStream)

        {

            string line = rdr.ReadLine();

            //do something with the line

        }

        rdr.Close();
 
 

        //METHOD 2, read the entire file then use code to split it up

        System.IO.StreamReader rdr = new System.IO.StreamReader(filename);

        string allText = rdr.ReadToEnd();

        string[] lines = allText.Split('\n'); //<--split by newlines, but could also split by commas etc

        foreach (string line in lines)

        {

            //do something with the line

        }

        rdr.Close();

Open in new window

0
 
LVL 15

Expert Comment

by:Solar_Flare
ID: 24760470
Once you have all the data that you have parsed out, you could use it to fill a datatable or write a .csv file (excel can read .csv files)


eg. if you wanted to put each line from the file into the next cell in excel

        System.Text.StringBuilder b = new System.Text.StringBuilder();
 

        System.IO.StreamReader rdr = new System.IO.StreamReader(filename);

        string allText = rdr.ReadToEnd();

        string[] lines = allText.Split('\n'); //<--split by newlines, but could also split by commas etc

        foreach (string line in lines)

        {

            b.Append("\""); //add opening quote around the value 

            b.Append(line);

            b.Append("\","); //add closing quote around the value, the comme 

        }

        rdr.Close();
 

        //at this point the stringbulider contains your one-line csv

       

        string csv = b.ToString(); // put the csv into a variable if you want
 

        //or write the csv out to a file

        System.IO.StreamWriter wr = new System.IO.StreamWriter("c:\destination.csv", false);

        wr.Write(b.ToString());

        wr.Close();

Open in new window

0
 
LVL 9

Expert Comment

by:Rahul Goel
ID: 24760770
After reading the file you can apply the regular expression to filter out the data!
and here you go!
System.IO.StreamReader rdr = new System.IO.StreamReader(filename);

string allText = rdr.ReadToEnd();

Open in new window

0
 
LVL 1

Author Closing Comment

by:cotj73
ID: 31599054
Great Help Thank You
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why, and when, to use Windows Workflow 1 31
Stream.BeginRead and Stream.EndRead in .NET Core 5 35
Class object 2 26
how to read json value 2 29
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now