Hi,
If I wanted to make sure a .txt file had certain lines in when opening what would be the best way to search it
for example If I wanted to test for something like "#test1" and "#test2" are contained what would be the easiest way to do this?
Printable View
Hi,
If I wanted to make sure a .txt file had certain lines in when opening what would be the best way to search it
for example If I wanted to test for something like "#test1" and "#test2" are contained what would be the easiest way to do this?
Read the file and compare the lines read to the target strings.
Im a little further along but im hitting a speedbump
I have a file that reads something like
#area1
item1
item2
item3
#
#area2
item4
item5
item6
#
what im trying do is populate a text box with the items from a specific area
so if i ran ReadIn("#area2",#,textbox2)
i should see items 4 5 6 in textbox2
I can get the loop to read in 1 line for specific areas but im unclear how to keep the loop going until it reaches the #
c# Code:
private void readin(string header,String ender,TextBox textbox) { try { StreamReader fileReader = File.OpenText(filename); string currentLine = ""; // read each line from the file while ((currentLine = fileReader.ReadLine()) != "# EOF") { if (currentLine.IndexOf(header) >= 0) { //loop to keep reading until ender is reached string looptest= fileReader.ReadLine().Trim(); while (looptest != ender && looptest !="# EOF") { textbox.AppendText(fileReader.ReadLine().Trim() + "\r\n"); } break; } } // close the stream reader fileReader.Close(); } catch { } }
Please give us the EXACT format of your text file otherwise it's just wasting our time giving you code that won't work because the real text format is not the same as in the example provided.
like i said
its a subrutine so i can pick whatever i want
but the current text is
Code:#writeins (Name)
Robert
Rhapsody
Tim
#
#mayoral races (City,State)
Fairfax
Centreville
Manassas
#
#EOF
so when its all said in done
the line
RaceIn("#writeins (Name)","#",textbox1);
should return
Robert
Rhapsody
Tim
to the textbox1
currently it is just returning
"rhapsody"
and the line RaceIn("#mayoral races (City,State)","#",textbox2)
returns
"Centreville
#"
Try this
c# Code:
private void ReadIn(String header, String ender, TextBox textbox) { string filename = @"C:\test.txt"; if (File.Exists(filename)) { try { StreamReader fileReader = File.OpenText(filename); string currentLine = ""; // read each line from the file while (currentLine != "#EOF") { //read a line currentLine = fileReader.ReadLine(); //test the line to see if it is the header line if (currentLine.IndexOf(header) >= 0) { //read the next line currentLine = fileReader.ReadLine(); while (currentLine != ender && currentLine != "#EOF") { textbox.AppendText(currentLine + Environment.NewLine); //loop to keep reading until ender is reached currentLine = fileReader.ReadLine(); } break; } } // close the stream reader fileReader.Close(); } catch { } }
I just tested it an it appears to work perfectly
Im going to spend a few hours comparing notes and i might have a few questions but thanks.