Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange Things Happening When Reading Data From A File

Status
Not open for further replies.

Smeat

Programmer
Mar 27, 2004
193
0
0
GB
Hi All

When reading data from a text file using the StreamReader class it is adding unwanted characters to the data.

When I read my text file using NotePad it looks like this:


"","","1-RF98G","ENU","","Jones",


When I read it into a string variable using the following code:

Code:
System.IO.StreamReader s = System.IO.File.OpenText(ImportFile);

  int counter = 0;
  while (s.Peek() > 0)
  {
    counter ++;
    //Loop through each record
    string @row = s.ReadLine();

    ....

The contents of my string variable look like this:


"\"\",\"\",\"1-RF98G\",\"ENU\",\"\",\"Jones\",


Anybody know why this is happening?

Anybody know how to stop this happening?

TIA

Smeat
 
if this is displayed in the watch window, don't worry,
it's just the way visual studio displays quotes inside strings.

the extra slashes aren't really there.

write the strings out again and you'll see.


mr s. <;)

 
Unfortunately this is not just in the watch window, these additional characters are being assigned to my variables so when I do comparisons with expected values the comparisons fail.

Any ideas ?

Ta

Smeat
 
will it work as thought if you change the line
Code:
string @row = s.ReadLine();
to
Code:
string row = s.ReadLine();

??

Age is a consequence of experience
 
Unfortunately this makes no difference.
 
Did you try/Does the following help?

Code:
string @row = s.ReadLine().ToString();
 
I guess there are a couple options that I can see...

1. Before writing the data to a file do a myString.Replace... and use a different character such as a tilde?
Code:
char apos = (char) 34;
char replace = ( char ) 126;
myString.Replace( apos, replace );


2. Change the string values to bytes i.e.
Code:
byte[] myChars = (byte) myString.ToCharArray()
(not tested or preferred)

maybe some others..

Age is a consequence of experience
 
If you are trying to save the string in a consistent manner that is not going to cause havoc, then look at the web classes. When you write in HTML you use tags like &quote

There are classes that can do this conversion for you in both directions.

 
It's almost certainly Visual Studio putting those characters in there in the debugger.

To prove this, after reading from your file, immediately copy the contents of your stream to another file, and open it in Notepad. It won't have the extra slashes.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top