Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 22nd, 2007, 01:10 AM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reading text files

 HI Guyz,

               I'm a newbie in asp.net. What i would like to do is read a text file , take certain values and post them back in a page.



For example let say a a.txt file which contains :



name :

senthil

age :

20

jjjjjjj

llllllll

name :

jenna

age :

25

designation :

teacher

llllllllllll

-----------------------------------------------------------------------





i would like to take only the values next to line with name and age only :::: name (jenna,senthil) age:(20,25)

and post them back in a new page.



i could read the whole file and post everything.:



<%@ Import Namespace="System.IO" %>

<SCRIPT Runat="Server">

Sub Read_File_Fields (Src As Object, Args As EventArgs)

  Dim FileReader As StreamReader
        FileReader = File.OpenText("C:\Documents and Settings\user1\My Documents\Works Senthil\Celcom Billing\17.Celcom - Billing\02.Celcom-Dell\TH.dat")

  Dim LineIn As String
  Dim FieldArray() As String
  Dim Field As String

  LineIn = FileReader.ReadLine()
  If Not LineIn = Nothing Then
            FieldArray = Split(LineIn, ",")

    '-- Display table header
    FileContents.Text &= "<table border=""1"">"
    FileContents.Text &= "<tr style=""background-color:#F0F0F0"">"
    For Each Field in FieldArray
      FileContents.Text &= "<th>" & Field & "</th>"
    Next
    FileContents.Text &= "</tr>"

    '-- Display table rows
    LineIn = FileReader.ReadLine()
    While LineIn <> Nothing
      FieldArray = Split(LineIn, ",")

      '-- Display a table row
      FileContents.Text &= "<tr>"
       For Each Field in FieldArray
       FileContents.Text &= "<td>" & Field & "</td>"
      Next
      FileContents.Text &= "</tr>"

      LineIn = FileReader.ReadLine()
    End While

    FileContents.Text &= "</table>"
  End If
  FileReader.Close()

End Sub

</SCRIPT>

<form id="Form1" Runat="Server">

<b>Read Celcom Bills Fields: </b>
<asp:Button Text="Read File" OnClick="Read_File_Fields" Runat="Server"/>

<p><asp:Label id="FileContents" EnableViewState="False" Runat="Server"/></p>

</form>
  ----------

How could i pick only a few values such as the name and age. The values for name is on the next line. Help me out if there is a way. I'm using vwd with sql express.the place i'm stuck is how to search for the line says (age/name) and find for the next line. i could noy picturize how the codes could look like. thank you for ya help.

 
Old May 22nd, 2007, 08:32 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You are headed in the right direction.

Create an enum that contains at least these:
   Name, Age, Unknown
You could call this enum 'LineType'.

As you read each line, you test the line for the keyword you are looking for (i.e. 'name', 'age'). As you encounter the keywords you could set a variable of the enum type that tells you what the next line value will be. Then on the next line read in the loop you take the value from the line and store that as the desired value.

One thing that will simplify this is to create a class to store the values you are going to pull out. As you encounter each new instance of data (new name) you can create a new instance of the class and add it to a collection/list.

One trick I use is to think backwards when you are working with loops. The best way to explain is to provide a little pseudo code:

for each line
   what line was encountered last time around?
      do the appropriate action based on linetype
   what is the current line value?
      assign line type based on keyword found
next line

The first pass thru the loop will also be linetype unknown so you'll just skip doing anything. But then you'll test for the keyword and start processing the lines on subsequent loops.


-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading DBF Files SerranoG Access VBA 5 May 11th, 2005 07:33 PM
Reading excel and text files using VB6... CJ35 VB How-To 5 October 8th, 2004 03:56 AM
Reading from Binary Files jschmi J2EE 0 October 5th, 2004 01:09 AM
Regarding " reading text files" spraveens Classic ASP Basics 1 October 14th, 2003 07:46 AM
reading files nightsurfer JSP Basics 0 August 10th, 2003 04:34 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.