473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

displaying text file block by block in c#

i have a text file that is divided into blocks:
#1
1:animal
2:food
3:moves
4:oxygen
#2
1:eats:2
2:breaths:2
3:can:1
4:is a:1
#3
1:1:2
1:3:3
1:2:4
and the following program that displays block by block:
Expand|Select|Wrap|Line Numbers
  1. StreamReader streamReader = new StreamReader("G:\\LAB123\\LABWORK2.txt");
  2.             int block = 0;
  3.             while (!streamReader.EndOfStream)
  4.             {
  5.                 string line = streamReader.ReadLine().Trim();
  6.                 if (line.CompareTo("") == 0)
  7.                 {
  8.                     continue;
  9.                 }
  10.                 if (line.CompareTo("#1") == 0)
  11.                 {
  12.                     block = 1;
  13.                     continue;
  14.                 }
  15.                 if (line.CompareTo("#2") == 0)
  16.                 {
  17.                     block = 2;
  18.                     continue;
  19.                 }
  20.                 if (line.CompareTo("#3") == 0)
  21.                 {
  22.                     block = 3;
  23.                     continue;
  24.                 }
  25.                 if (block == 1)
  26.                 {
  27.                     string[] pairs = line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  28.                     if (pairs.Length == 2)
  29.                     {
  30.                         int code = Convert.ToInt32(pairs[0].Trim());
  31.                         string name = pairs[1].Trim();
  32.                         Console.WriteLine("code = {0}, name = '{1}'", code, name);
  33.                     }
  34.                 }
  35.                 if (block == 2)
  36.                 { 
  37.                   string[] pairs = line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  38.                   if (pairs.Length == 3)
  39.                   {
  40.                       int code = Convert.ToInt32(pairs[0].Trim());
  41.                       string name = pairs[1].Trim();
  42.                       int type = Convert.ToInt32(pairs[2].Trim());
  43.                       Console.WriteLine("code = {0}, name = '{1}', type = {2}", code, name, type);
  44.                   }
  45.                 }
  46.                 if (block == 3)
  47.                 {
  48.  
  49.                  string[] pairs = line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  50.                  if (pairs.Length == 3)
  51.                   {
  52.                      int code = Convert.ToInt32(pairs[0].Trim());
  53.                      int connection = Convert.ToInt32(pairs[1].Trim());
  54.                      int types = Convert.ToInt32(pairs[2].Trim());
  55.                      Console.WriteLine("code = {0}, connection = {1}, types = {2}", code, connection, types);
  56.                     }  
  57.                 }
my problem is am supposed to add a verification code so that if i put a string e.g 1:?:? it returns the block where the string is found and gives of all possible answers please help
Attached Files
File Type: txt LABWORK2.txt (240 Bytes, 347 views)
Dec 21 '12 #1
2 3087
Rabbit
12,516 Expert Mod 8TB
I have no idea what you're trying to do. Is this homework?
Dec 21 '12 #2
The code above displays my text file into block 1,2,3.but i need to search a specific string eg 1:?:? and related terms like 1:1:2, 1:eat:2, will be displayed on screen.
Dec 21 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Imran | last post by:
hello all, I have to parse a text file and get some value in that. text file content is as follows. ####TEXT FILE CONTENT STARTS HERE ##### /start first 0x1234 AC /end
1
by: semi | last post by:
Hello, I am using VS C++ to make a gui that displays a text file contents from main dialog. So I created main dialog and another dialog for text file viewer. From the main dialog, I call text...
1
by: jlea | last post by:
Does anyone know of a nice little .NET class that throws the contents of a supplied file to a scrollable window/form/control?
5
by: Amjad Farran | last post by:
What is the easiest way to display in a table, a delimited text data saved in a text file? I need an idea so that I can research it! Amjad
3
by: Rosario Morgan | last post by:
Hello Help is great appreciated in advance. I need to loop through a file 6000 bytes at a time. I was going to use the following but do not know how to advance through the file 6000 bytes at...
13
by: sugard | last post by:
Abstract Class, Inheritance, Polymorphism, File Handling, Exception Handing A program is to be devised for a university to work out whether or not, a student or a professor, is outstanding. The...
1
by: CAM123 | last post by:
I have added: <br><xsl:value-of select="Line" /></br> to my XSLT stylesheet to get a line per repeating block. When I view the output as XML it looks perfect - one line per block. However...
0
by: paragguptaiiita | last post by:
I have one problem regarding displaying text from XML file. I have a XSL variable named $p = (0 0 0) (0.82 0 0) (1.63 -0.01 0) (2.63 -0.01 0) (3.63 -0.01 0) (4.63 -0.01 0) (5.63 -0.02 0) (6.63 -0.02...
0
by: paragguptaiiita | last post by:
I have one problem regarding displaying text from XML file. I have a XSL variable named $p = (0 0 0) (0.82 0 0) (1.63 -0.01 0) (2.63 -0.01 0) (3.63 -0.01 0) (4.63 -0.01 0) (5.63 -0.02 0) (6.63 -0.02...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.