22 Replies - 986 Views - Last Post: 08 October 2011 - 09:33 AM
#1
Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:04 AM
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.
Replies To: Read a text file and extract some part of the File!
#2
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:11 AM
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?
#3
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:26 AM
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!
#4
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:31 AM
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
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] + " ";
}
}
#5
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:32 AM
This post has been edited by tlhIn`toq: 05 October 2011 - 06:34 AM
#6
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:33 AM
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.
#7
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:36 AM
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
DarenR, on 05 October 2011 - 06:32 AM, said:
YES, It is so
#8
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:37 AM
DarenR, on 05 October 2011 - 07:32 AM, said:
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
#9
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:40 AM
Thanks for catching my typo. It is hard enough to understand foreigners without me mispelling easy English words.
#10
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:41 AM
tlhIn`toq, on 05 October 2011 - 06:33 AM, said:
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!
#11
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:44 AM
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.?

See FAQ #5 for debugging tutorials.
FAQ (Frequently Asked Questions - Updated Sep 2011
behrooz.09, on 05 October 2011 - 07:41 AM, said:
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.
#12
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:53 AM
but the problem is that I want to print the
This post has been edited by tlhIn`toq: 06 October 2011 - 11:14 AM
#13
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 06:56 AM
#14
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 07:40 AM
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.
#15
Re: Read a text file and extract some part of the File!
Posted 05 October 2011 - 10:36 AM
|
|

New Topic/Question
Reply



MultiQuote






|