C# - This is truly irritating! BUGGY?

EFileTahi-A

Well-known member
Joined
Aug 8, 2004
Messages
539
Location
Portugal / Barreiro
Code:
StreamWriter strWriter = new StreamWriter(Application.StartupPath + "\\" + "bla.txt", false, System.Text.Encoding.BigEndianUnicode);
strWriter.WriteLine(textBox1.Text);
strWriter.WriteLine(textBox2.Text);
strWriter.WriteLine(textBox3.Text);
strWriter.WriteLine(textBox4.Text);
strWriter.WriteLine(textBox5.Text);
strWriter.WriteLine(textBox6.Text);
strWriter.WriteLine(textBox7.Text);
strWriter.WriteLine(textBox8.Text);
strWriter.Close();

This creates a text file with following input:
1
2
3
4
5
6
7
8

Ok, but I want to read every line to a specific string variable. Why the F. does this F. S. reads only a few lines? why is it skiping lines?

Code:
string sTemp1 = strReader.ReadLine(); //Assigns value 2
string sTemp2 = strReader.ReadLine(); //Assigns value 4
string sTemp3 = strReader.ReadLine(); //Assigns value 6 
string sTemp4 = strReader.ReadLine(); //Assigns value 8
string sTemp5 = strReader.ReadLine(); //Null!
string sTemp6 = strReader.ReadLine(); //Null!
string sTemp7 = strReader.ReadLine(); //Null!
string sTemp8 = strReader.ReadLine(); //Null!

???

Strangely enough, this it reads the text file well if I assign the values to textboxes instead!

Code:
this.textBox1.Text = strReader.ReadLine(); //Assigns value 1
this.textBox2.Text = strReader.ReadLine(); //Assigns value 2
this.textBox3.Text = strReader.ReadLine(); //Assigns value 3
this.textBox4.Text = strReader.ReadLine(); //Assigns value 4
this.textBox5.Text = strReader.ReadLine(); //Assigns value 5
this.textBox6.Text = strReader.ReadLine(); //Assigns value 6
this.textBox7.Text = strReader.ReadLine(); //Assigns value 7
this.textBox8.Text = strReader.ReadLine(); //Assigns value 8

WHY!?

Well if needed it will be pleasure to upload the project...
 
In your load method change
C#:
StreamReader strReader = new StreamReader(fs);
to
C#:
StreamReader strReader = new StreamReader(fs, System.Text.Encoding.BigEndianUnicode);
and it should work.
 
Er... It does not work. Here is a shot of what happens:

fd9c2852a9a01b2971f702e373ab592a.jpg


sTemp4 var is reading "8" when it should be holding "4". The rest of the vars (sTemp5, 6, 7 and 8) become null just like I described in the threads opening post...

I did not change anything from the project you post... I truly dont know what to do next...
 
Just re-ran it here and used both the bla.txt contained in the zip and also got the code to generate a new file.
See attached screen shot to see all variables loaded correctly. I really cant see why it should fail on your pc as I have tried it on both 1.0 and 1.1 frameworks.

Are you doing anything with the text file before loading it?
 
Well, Im truly pisst off with this **** it always advance 2 lines. When the code focus on the first break point at sTemp1 the Readline holds "1", then, when I press F11 sTemp1 turns to 2 and the Readline becomes "3", sTemp2 will then hold 4 and Readline will read "5"... I give up... Thanks for your effort though.
 
IngisKahn ***

lol
Look at your watch window.

bri189a ***

Yup, that would do it. That is TOO funny!

Too funny? And why is that? You mean the "Function strReader.ReadLine evaluted and returned null" message?

Have you guys (just for statistics) read carefully my first post? If so then you will both realise that strReader.ReadLine will be null after assigning sTemp4 string var. Plus, why does it work OK if using "strReader.ReadLine()" directly on textboxes instead of string vars? Yet, why does the project work fine at PlausibleDump sys and not at mines? Have you also read my last post where I explain the strange behaviour of strReader.ReadLine() advancing 2 lines at the time?

Also...

I brough the project to my jobs computer and it works just fine... I wonder why it does not work at home...

Here at my Jobs VS .NET I can see the "locals" tab, at home I cant see it anywhere. How do I make it appear?

Now, I am truly looking forward to listening to your theories once more (IngisKahn and bri189a comments). Probably another "genuine" lol as an answer...

And just to finish. When I drag with mouse the "next line to read" (yellow line) back and forth over the sTemp1, 2, 3, 4, 5, 6, 7 and 8 the strReader.ReadLine() will automaticaly be executed, that is, it will advance for the next line from "bla.txt". I can see its value change on the "watch tab" without me pressing F11 or F10, hell, without me pressing any key at all. What do you make of this?
 
Last edited by a moderator:
Could you attach a copy of the text file that isnt reading correctly?
Also if you take a text file from home to work can you read it okay? How about the other way round?

Do you have any additional language / keyboard support installed on your home PC?
 
Sorry, it one of those things Ive done myself, (and probably the other) guy, and Ive wasted time looking for...because I wasnt thinking about what the debug commands were doing the variables; it wasnt meant to be a jab at you...sorry...more of a laugh of hey, Im not the only one who does this!, and I think we both thought you would see the problem if we pointed your focus to the watch window, I again I appologize.

Its advancing twice because once it advances in your code, but then the watch advances it one too because the reader in the watch is by reference...now if its still doing it after you remove the watch, then then I humbly withdraw my comment, but that is what seems to be the obvious problem as it stands now.
 
PlausiblyDamp said:
Could you attach a copy of the text file that isnt reading correctly?
Also if you take a text file from home to work can you read it okay? How about the other way round?

Do you have any additional language / keyboard support installed on your home PC?

Well I will take my .txt which was made here (at my job) back to home and see what happens. At home I think I have only one keyboard installed (two at best, portuguese and enlgish keyboard maps. But I doubt I have the english one, I always delete it after installing the sys)...

PS: Where can I add the "LOCALS" tab? I have it here, but not at home...
 
Jeez, lighten up. :) = happy/fun/good/nice Have you read my post? You have a function call causing side effects in the watch window. (BTW, VS 2005 wont evaluate functions with side effects in the debug windows.)

Debug->Windows lets you display all the debug windows.
 
bri189a said:
Sorry, it one of those things Ive done myself, (and probably the other) guy, and Ive wasted time looking for...because I wasnt thinking about what the debug commands were doing the variables; it wasnt meant to be a jab at you...sorry...more of a laugh of hey, Im not the only one who does this!, and I think we both thought you would see the problem if we pointed your focus to the watch window, I again I appologize.

Its advancing twice because once it advances in your code, but then the watch advances it one too because the reader in the watch is by reference...now if its still doing it after you remove the watch, then then I humbly withdraw my comment, but that is what seems to be the obvious problem as it stands now.

Apologies accepted and that subject is already forgotten. Anyway, it does happen even after me removing the watch... What truly drives me mad is that everything works ok when I use textboxes to hold the RealLine() instead of string vars, even the watch will advance ok without skiping lines.
 
Have you tried declaring the string variables BEFORE you assign values to them?

Code:
string tempvar1;
string tempvar2;
string tempvar3;
//... ... ...

tempvar1 = strReader.ReadLine()
tempvar2 = strReader.ReadLine()
//...
 
If you put a watch on the variable rather than the .ReadLine() call does it work? It looks like the watch windows is calling the ReadLine method (which makes sense as that is the only way it can retrieve the data)
 
thenerd said:
Have you tried declaring the string variables BEFORE you assign values to them?

Code:
string tempvar1;
string tempvar2;
string tempvar3;
//... ... ...

tempvar1 = strReader.ReadLine()
tempvar2 = strReader.ReadLine()
//...

No, I did not, but even if this worked why should the other method work in all computers except mine?

God damn... what is happening?. Here at my job it works JUST perfectly the minute I try it at home it will just act strange...
 
Back
Top