Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text document and it contain lines like

Problem Steps: 7 asdasfdgf.....{Here i can find total number of steps}

[steps starts here ]

Problem Step 1: aaaaaaaaa
Problem Step 2: bbbbbbbb
Problem Step 3: cccccccc
Problem Step 4: ddddddd
Problem Step 5: eeeeeee
Problem Step 6: ffffffff
.
.
.
.
Problem Step n: nnnnnnnnn

How to read each line by line from the text document based on the total number of steps and save in a list or array

Thanks in advance
Posted
Comments
johannesnestler 4-Dec-14 9:36am    
I think you are faster if you sit down and: read the docu about System.File as RyanDev suggested, then write code to read the line (first?) where your "Problem steps count" is written. Jumpt to "steps start" and read as many "steps" as you need. Finished. You need more time for commenting here than to solve your problem...

You can use the System.File[^] class. It has, for example, a method named ReadAllLines().

Just read them in and do what you need to do.
 
Share this answer
 
Comments
Renjith_R 4-Dec-14 9:31am    
Thanks for quick reply,
lets say i have 10 lies start with step1,step2,step3,step4,step5 ..... step10.
i want to extract these steps line by line and store in a list.
i know how many steps will be there in .txt file.
ZurdoDev 4-Dec-14 9:33am    
OK. So where are you stuck? Call ReadAllLines and then do what you want with the data.
Like this:
C#
List<string> dataList = new List<string>();

using(StreamReader sr = new StreamReader("data.txt"))
{
    while(sr.Peek() > 0)
    {
        dataList.Add(sr.ReadLine());
    }
    while(sr.Peek() > 0)
    {
        string temp = sr.ReadLine();

        if(temp.StartsWith("Problem Step"))
        {
            dataList.Add(temp);
        }
    }
}
 
Share this answer
 
v3
Comments
Renjith_R 4-Dec-14 9:31am    
Thanks for quick reply,
I don't want to read all lines from the .txt file and store to a data list :(
lets say i have 10 lies start with step1,step2,step3,step4,step5 ..... step10.
i want to extract these steps line by line and store in a list.
i know how many steps will be there in .txt file.
TheRealSteveJudge 4-Dec-14 9:44am    
You're welcome!
I adapted the example.
Now it is checked if a line starts with "Problem Step".
Only matching lines are added to the list.
There is no need to know the count.
Renjith_R 4-Dec-14 9:58am    
Thanks Dude,Its working :)
TheRealSteveJudge 4-Dec-14 10:02am    
Your're welcome!
johannesnestler 4-Dec-14 9:32am    
if you read the whole file and really need a List (not an Array) you could write all like this: List<string> listData = new List<string>(File.ReadAllLines(strPath)); (one line) anyway OP needs to read until his condition is met...

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