Results 1 to 7 of 7

Thread: [RESOLVED] Test .txt for text

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Test .txt for text

    Read the file and compare the lines read to the target strings.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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:
    1. private void readin(string header,String ender,TextBox textbox)
    2.         {
    3.             try
    4.             {
    5.                 StreamReader fileReader = File.OpenText(filename);
    6.                 string currentLine = "";
    7.  
    8.                 // read each line from the file
    9.                 while ((currentLine = fileReader.ReadLine()) != "# EOF")
    10.                 {
    11.                     if (currentLine.IndexOf(header) >= 0)
    12.                     {
    13.                         //loop to keep reading until ender is reached
    14.                        string looptest= fileReader.ReadLine().Trim();
    15.                         while (looptest != ender && looptest !="# EOF")
    16.                         {
    17.                            
    18.                             textbox.AppendText(fileReader.ReadLine().Trim() + "\r\n");
    19.                         }
    20.                         break;
    21.                     }
    22.                 }
    23.                 // close the stream reader
    24.                 fileReader.Close();
    25.             }
    26.             catch
    27.             { }
    28.        
    29.        
    30.        
    31.         }

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.

  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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.

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Test .txt for text

    Try this
    c# Code:
    1. private void ReadIn(String header, String ender, TextBox textbox)
    2.         {
    3.             string filename = @"C:\test.txt";
    4.             if (File.Exists(filename))
    5.             {
    6.                 try
    7.                 {
    8.                     StreamReader fileReader = File.OpenText(filename);
    9.                     string currentLine = "";
    10.  
    11.                     // read each line from the file
    12.                     while (currentLine != "#EOF")
    13.                     {
    14.                         //read a line
    15.                         currentLine = fileReader.ReadLine();
    16.                         //test the line to see if it is the header line
    17.                         if (currentLine.IndexOf(header) >= 0)
    18.                         {
    19.                             //read the next line
    20.                             currentLine = fileReader.ReadLine();
    21.                             while (currentLine != ender && currentLine != "#EOF")
    22.                             {
    23.                                 textbox.AppendText(currentLine + Environment.NewLine);
    24.                                 //loop to keep reading until ender is reached
    25.                                 currentLine = fileReader.ReadLine();
    26.                             }
    27.                             break;
    28.                         }
    29.                     }
    30.                     // close the stream reader
    31.                     fileReader.Close();
    32.                 }
    33.                 catch
    34.                 { }
    35.             }

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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
  •  



Click Here to Expand Forum to Full Width