Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hiya,

I have a question about loading and parsing files on C#, I am trying to load and parse 3 files, File01.txt, File02.txt, File03.txt, how would I come about doing that?

What I have tried:

I have tried regex, but most people have used StreamReader, I'm still a novice at coding, so I'm not sure
Posted
Updated 6-Dec-16 11:48am
v2
Comments
Patrice T 6-Dec-16 17:59pm    

1 solution

After gazing into my crystal ball in an effort to divine the contents of said text files, I got nothin.

Loading a text file (or finding out how to) is beyond the trivial.

C#
using (StreamReader stream = new StreamReader("TestFile.txt"))
{
    String text = stream.ReadToEnd();
}
if (!string.isNullOrEmpty(text)
{
    string[] lines = text.Split('\n');
    string lines[];
    foreach(string line in lines)
    {
        // do somthing with your line of text
    }
}


I recommend that you work on your google foo.

EDIT: MTH 2016-12-05
You have a variable's scope problem (String text).
It will fail on files that don't fit in memory.
And it could be a bit simpler.
C#
foreach (string line in File.ReadLines("TestFile.txt"))
{
  // do something with your line of text
}
 
Share this answer
 
v3
Comments
pt1401 3-Dec-16 8:43am    
Oh come on.
If you expect people to help you, at least give us a chance.
I get that you want to parse these files using C#, but what language is it that is in the text files that you want to parse?

"What I have tried: I have been struggling for the right code"
But you can't give us any more detailed insight into your struggles?
Cut & paste your attempts below...
Dave Kreskowiak 3-Dec-16 9:50am    
Be careful who you reply to. You did not reply to the original poster. You replied to John.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900