I'm reading line by line from a text file but in the messagebox.show I see the same first line all the time
In the constructor I did:
binFileToRead = @"d:\DenyTextHaunted.bin";
r = new StreamReader(binFileToRead);
f = r.ReadToEnd();
translatedFile = @"d:\binText_1.txt";
h = new StreamReader(translatedFile);
test();
Now the code in test()
private void test()
{
StreamWriter w = new StreamWriter(@"d:\testFile.txt");
w.AutoFlush = true;
int currentLength;
int currentIndex;
int lastIndex = 0;
string startTag = "<Text>";
string endTag = "</Text&开发者_C百科gt;";
int startTagWidth = startTag.Length;
//int endTagWidth = endTag.Length;
int index = 0;
while ((line = h.ReadLine()) != null)
{
while (true)
{
index = f.IndexOf(startTag, index);
if (index == -1)
{
//startTag not found, but endTag may still be found
index = f.IndexOf(endTag, lastIndex - 1);
if (index != -1)
{
w.Write(f.Substring(lastIndex, endTag.Length));
}
break;
}
// else more to do - index now is positioned at first character of startTag
int start = index + startTagWidth;
if (start >= f.Length)
break;
currentIndex = start;
index = f.IndexOf(endTag, start + 1);
if (index == -1)
{
break;
}
// found the endTag
string g = f.Substring(start, index - start - 1);
currentLength = index - start;
string hh = f.Substring(lastIndex, currentIndex - lastIndex);
w.Write(hh);
w.Write(line);
MessageBox.Show(line.ToString());
listBox2.Items.Add(hh);
lastIndex = currentIndex + currentLength;
listBox1.Items.Add(g);
}
w.Close();
}
}
What I did in test() so far is reading one file to the end and then taking out all the file content without the text between the tags <Text>
and </Text>
And wrote this to a new text file.
So the new text file for example looks like:
hi everyone <Text></Text> and you are...
<Text></Text> by and hello
Now what I wanted to do is to read line by line from another file which is variable h
and I added this while ((line = h.ReadLine()) != null
)
Then I'm writing each line to the place in between the <Text></Text>
w.Write(line);
But in the MessageBox.Show(line.ToString());
I see the first line of the text file all the time.
I'm reading and repeating all the time showing the first line only.
Why doesn't it show me the next file in the next iteration ?
Thanks.
Guffa it dosent working good. I used your repair sample using one while loop to read line by line but then the written new file is missing some lines from the end. Im not sure if the problem is in the written or in the reading to the end in the constructor. Or maybe the loop isnt good.
What i wanted to do is to read some of F untill the point of the read some of H one line and then put this line between the first then that F will keep reading to the next then reading from H the next second line and then put the second line in the second place of
So its working in general the concept is working but technicaly it dosent its reading or writing to the new file almost to the end but missing few lines.
private void test()
{
StreamWriter w = new StreamWriter(@"d:\testFile.txt");
w.AutoFlush = true;
int currentLength;
int currentIndex;
int lastIndex = 0;
string startTag = "<Text>";
string endTag = "</Text>";
int startTagWidth = startTag.Length;
//int endTagWidth = endTag.Length;
int index = 0;
while ((line = h.ReadLine()) != null)
{
//while (true)
//{
index = f.IndexOf(startTag, index);
if (index == -1)
{
//startTag not found, but endTag may still be found
index = f.IndexOf(endTag, lastIndex - 1);
if (index != -1)
{
w.Write(f.Substring(lastIndex, endTag.Length));
}
break;
}
// else more to do - index now is positioned at first character of startTag
int start = index + startTagWidth;
if (start >= f.Length)
break;
currentIndex = start;
index = f.IndexOf(endTag, start + 1);
if (index == -1)
{
break;
}
// found the endTag
string g = f.Substring(start, index - start - 1);
currentLength = index - start;
string hh = f.Substring(lastIndex, currentIndex - lastIndex);
w.Write(hh);
w.Write(line);
listBox2.Items.Add(hh);
lastIndex = currentIndex + currentLength;
listBox1.Items.Add(g);
//}
//w.Close();
}
w.Close();
}
f is a file in the constructor im reading to the end:
binFileToRead = @"d:\DenyTextHaunted.bin";
r = new StreamReader(binFileToRead);
f = r.ReadToEnd();
And h is another text file im reading in the constructor but no to the end :
translatedFile = @"d:\binText_1.txt";
h = new StreamReader(translatedFile);
Then in the function test()
im creating a new text file that should contain all the content of the file F without the text between the tags and
And im writing to this file also the h file lines line by line:
First its reading some of F until the first and then its reading one line from H and put the line between the
And so on.
But for some reason when im editing the new text file testFile.txt i see that it didnt write to the end.
The last line is for example like this:
Hmm smells not badly, but you do not have a cirque-red and the following à ¼r micht please... grr.
But there is no in the end and i checked the original file there are more few lines after this line.
So why its reading the original file or writing to the new file only untill this part ?
Maybe since the loop im using is finishing when its finish to read all lines from the H file ?
Before it i had another loop i used it was just While(true){}
But then it didnt read line by line from H
Im not sure why its not writing or reading to the end.
The concept is working but technicaly its not getting to the end.
Here is example of how the H im reading is looks like:
The barrel also does not help me here. The barrel makes here really no sense. Here pity also makes the barrel no sense. The barrel makes no sense with the poster. Only I must create good idea, it anyhow after drau à en... The ship k à ¶nnte are used anyhow, around the barrel to entz à ¼nden. But not under water. First not.
This is the file i want to readl ine by line.
The other file had another text between and now i want to take line by line as explained above reading some of this then reading some of this put a line and so on.
Thanks.
That is because you have two loops, and read data in the outer loop. As you never exit out of the inner loop (unless there is something wrong), you never get the second line.
You have this structure:
while ((line = h.ReadLine()) != null)
{
while (true)
{
...
}
w.Close();
}
but you should have:
while ((line = h.ReadLine()) != null)
{
...
}
w.Close();
I'm not sure what the purpose of the loop is anyway. You don't use the value of line
for any logic in the loop, so you will only be doing the same work over and over again, and then display the same result over and over again, along with the lines that you read from the file.
Instead of answering why your current solution does not work (for that, refer to Guffa's answer) I would like to ask why are trying to parse XML in C# line-by-line as text? There are far better solutions to do this, see linked articles below.
- MSDN How to: Parse XML with XmlReader
- How does one parse XML files?
Using a better method to parse your data will make the problem you need to solve much easier.
Note, there many many more articles on this subject - search google for 'read xml c#'.
HTH,
精彩评论