Read a text file and extract some part of the File!

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 5067 Views - Last Post: 08 October 2011 - 09:33 AM Rate Topic: -----

#1 behrooz.09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-October 11

Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:04 AM

Hello guys,
I have a text file which I have to read it from my code which is Ok.
THE PROBLEM :
the program has to read the file and just take and extract or print some part of the text file.
it means Strings between 2 Words.

Here is the content of the text file:
// #Name
// Behrooz 123_box_2000
// ...
// ...
// .....
// #Requirements
// lab_121, lab_222, lab_312
// ...
// ...
// .....
// #Name
// John 555_box_2010
// ...
// ...
// .....
// #Requirements
// lab_666, lab_819, lab_731

AND I need to print out just :

// #Name
// Behrooz 123_box_2000
// #Requirements
// lab_121, lab_222, lab_312

// #Name
// John 555_box_2010
// #Requirements
// lab_666, lab_819, lab_731


I appreciated if you can help me.
BR,
Behrooz.

Is This A Good Question/Topic? 0
  • +

Replies To: Read a text file and extract some part of the File!

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:11 AM

There is nothing to *help* you with as you have not provided any of your own effort.

Please provide the code you wrote making an attempt to do this, along with any errors or an explanation of why your code isn't getting the job done and we will try to steer you back on track.

But you say you are already reading the file.
So just loop through the lines you are reading and look for the text you need. What exactly is the problem if you are already reading the file?
Was This Post Helpful? 0
  • +
  • -

#3 behrooz.09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-October 11

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:26 AM

Here is my code :

1. class read_Search_2
2.    {
3.        public static void Main()
4.        {
5.            string tagStart = "// #NAME";
6.            string tagEnd = "// #";
7.            StreamReader myFile = new StreamReader(@"test.txt");
8.            string myString = myFile.ReadToEnd();
9.
10.            string[] array = myString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
11.            int start = (Array.IndexOf(array, tagStart)) + 1; //+1 means that we get rid of #NAME line
12.            int end = 0;
13.
14.            for (int i = start; i < myString.Length; i++)
15.            {
16.                string item = array[i];
17.                if (item == tagEnd)
18.                {
19.                    end = i;
20.                    break;
21.                }
22.            }
23.
24.
25.            string result = null;
26.            for (int i = start; i < end; i++)
27.                result += array[i] + " ";
28.        }
29.    }


I get the error in Line 16 which says(Index was outside the bounds of the array.)
and I don't get any result!
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:31 AM

Let me guess, you copied this right off the website where you got it?
That's the only reason I can image it has all the line numbers built in.

In the future please copy the code directly from Visual Studio and paste it into the thread, then use the code tags button to surround the code with the property tags
:code:

class read_Search_2
    {
        public static void Main()
        {
            string tagStart = "// #NAME";
            string tagEnd = "// #";
            StreamReader myFile = new StreamReader(@"test.txt");
            string myString = myFile.ReadToEnd();

            string[] array = myString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            int start = (Array.IndexOf(array, tagStart)) + 1; //+1 means that we get rid of #NAME line
            int end = 0;

            for (int i = start; i < myString.Length; i++)
            {
                string item = array[i];
                if (item == tagEnd)
                {
                    end = i;
                    break;
                }
            }


            string result = null;
            for (int i = start; i < end; i++)
                result += array[i] + " ";
        }
    }

Was This Post Helpful? 1
  • +
  • -

#5 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 463
  • View blog
  • Posts: 3,175
  • Joined: 12-January 10

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:32 AM

are the // actually in the text file? If so those lines are not empty. you would have to use a delimiter on tose those lines.

This post has been edited by tlhIn`toq: 05 October 2011 - 06:34 AM

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:33 AM

This is simply a horrible way to read a text file.
I recommend you read the MSDN tutorials on this where each line is read in to its own array element.

Writing a text file tutorial.
Reading a text file tutorial.
Was This Post Helpful? 0
  • +
  • -

#7 behrooz.09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-October 11

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:36 AM

in the text file the lines which start with // # wont be change but the most import part is the line after.
for instance:

// #Name
// Behrooz 123_box_2000

The second line is vary and I need to print it as well.
here I need to say start to print right after "// #NAME" until you reach "// #" Of course in programmatic way, which I don't know :(

View PostDarenR, on 05 October 2011 - 06:32 AM, said:

are the // actually in the text file? If so those lines are not empty. you would have to use a delimiter on tose those lines.

YES, It is so
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:37 AM

View PostDarenR, on 05 October 2011 - 07:32 AM, said:

are the // actually in the text file? If so those lines are not empty. you would have to use a delimiter on tose those lines.


I'm sure they are in the file.

The OP is trying to extract and keep everything between
// #Name
and
// #Requirements

presumably all the personal details about the student
Was This Post Helpful? 0
  • +
  • -

#9 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 463
  • View blog
  • Posts: 3,175
  • Joined: 12-January 10

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:40 AM

tlhIn`Toq ,

Thanks for catching my typo. It is hard enough to understand foreigners without me mispelling easy English words.
Was This Post Helpful? 0
  • +
  • -

#10 behrooz.09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-October 11

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:41 AM

View PosttlhIn`toq, on 05 October 2011 - 06:33 AM, said:

This is simply a horrible way to read a text file.
I recommend you read the MSDN tutorials on this where each line is read in to its own array element.

Writing a text file tutorial.
Reading a text file tutorial.

thanks for your comments mate,
I am not a programmer at all, I am a Designer and I made to take a course for graduation which is programming in .Net
right now I just need to do this task in a short time!
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:44 AM

Line 17 isn't going to work for you
if (item == tagEnd)


Because you will never match '// #' based on your example
The lines are
// #Name
and
// #Requirements

Basically you need to debug this. You have not really thought it through nor do I think you've spent much time actually 'debugging'. Are you familiar with breakpoints and walking through the code line by line as it executes and looking at the values of the variables.?


Attached Image

See FAQ #5 for debugging tutorials.


FAQ (Frequently Asked Questions - Updated Sep 2011

Spoiler


View Postbehrooz.09, on 05 October 2011 - 07:41 AM, said:

thanks for your comments mate,
I am not a programmer at all, I am a Designer and I made to take a course for graduation which is programming in .Net
right now I just need to do this task in a short time!


Reminder to all:
This is student homework. We can guide the OP, but not provide working code.

Was This Post Helpful? 0
  • +
  • -

#12 behrooz.09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-October 11

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:53 AM

That's right, when I change the tagEnd to "// #COUNTER" I don't get that error any more.
but the problem is that I want to print the the Name then Requirement. and skip the other part before, between and after these 2.

This post has been edited by tlhIn`toq: 06 October 2011 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 06:56 AM

I understand that's the assignment. So what's the problem?
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 07:40 AM

Let's clarify something here.

We're not going to do your homework. I understand that you're a designer that had to take a programming class. You don't care about learning C#. However, that doesn't mean we're going to do your homework for you, or even help you fix code you borrowed from another website.

Put forth an honest effort at solving the problem, and we'll be very happy to help you debug it and fix it until it works. But until you show that you're willing to at least try to do your own work, we can't help you.
Was This Post Helpful? 0
  • +
  • -

#15 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 463
  • View blog
  • Posts: 3,175
  • Joined: 12-January 10

Re: Read a text file and extract some part of the File!

Posted 05 October 2011 - 10:36 AM

sheesh i love getting random negative rep by people not involved in the conversation. --- my question was to verify the file nothing more nothing less.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2