|
-
Oct 17th, 2007, 12:34 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Test .txt for text
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?
-
Oct 17th, 2007, 12:49 AM
#2
Re: Test .txt for text
Read the file and compare the lines read to the target strings.
-
Oct 19th, 2007, 01:27 PM
#3
Thread Starter
Fanatic Member
Re: Test .txt for text
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
{ }
}
-
Oct 19th, 2007, 01:34 PM
#4
Re: Test .txt for text
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.
-
Oct 19th, 2007, 03:14 PM
#5
Thread Starter
Fanatic Member
Re: Test .txt for text
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
#"
Last edited by Crash893; Oct 19th, 2007 at 03:46 PM.
-
Oct 19th, 2007, 04:22 PM
#6
Re: Test .txt for text
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
{ }
}
-
Oct 19th, 2007, 10:24 PM
#7
Thread Starter
Fanatic Member
Re: Test .txt for text
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|