Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read File and get specific text from line

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
0
0
US
Hellow again,

I need to read a file(.txt) and get a particular row. the file containes a sngle row per order shipped.

This an sample of how the data looks. For this sample file I need to read the text file by using the tracking number 848257570000106 and when it findthe row with the tracking number I want either

A) the entire row or
B) the value between "1528," and "1596",

In this example I would either need the entire line or 706

Code:
'0,"120"1,"MPS Ship"10,"344002738"29,"848257570000080"34,"660"35,"46"1528,"325"1596,"0"1598,""'
'0,"120"1,"MPS Ship"10,"344002738"29,"848257570000093"34,"265"35,"46"1528,"362"1596,"0"1598,""'
[COLOR=Red]'0,"120"1,"MPS Ship"10,"344002738"29,"848257570000106"34,"879"35,"46"1528,"706"1596,"0"1598,""'[/color]
'0,"120"1,"MPS Ship"10,"344002738"29,"848257570000128"34,"155"35,"46"1528,"952"1596,"0"1598,""'
'0,"120"1,"MPS Ship"10,"344002738"29,"848257570000134"34,"681"35,"46"1528,"574"1596,"0"1598,""'

The end user woud supply an order number in a windows form which will populate the tracking numbers in a dropdown box for this orders packages. He will select a tracking number and this is the value that will be searched in the text file.

is this possible nad if so what would be the best way to do it. I am alos open to other sugegstion on how I cna get ths value from the text file

Thanks in advance
RJL
 
Hi RJL,

sounds pretty straight forward.
How abou something like this:
Code:
using (StreamReader sr = new StreamReader(pfad + "\\pk.xml"))
{
    string line = "";
    string trackingnumber="848257570000106";
    while (!sr.EndOfStream)
    {
        line = sr.ReadLine();
        if (line.IndexOf(trackingnumber) > 0)
        {
            //line now has the entire line
            string mycode = line.Substring(line.IndexOf("1528,") + 5, line.IndexOf("1596"));
            mycode = mycode.TrimStart(' ', '\"');
            mycode = mycode.TrimEnd(' ', '\"');
            break;
        }
    }
}

Would that do it?

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Could this line [tt]string mycode = line.Substring(line.IndexOf("1528,") + 5, line.IndexOf("1596"));[/tt]

not be extended to [tt]string mycode = line.Substring(line.IndexOf("1528,\"") + 5, line.IndexOf("\"1596"));[/tt]

and thus negate the need for the two Trim statements.

I don't have c# on this machine so I can't check, but it seems a simpler approach to me.
 
I guess so.
[bigcheeks]

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
RJL, do you ever respond to your threads??

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top