473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

StreamReader to read from string?

Hi,

Im using a code snippet that reads a text file from disc and processes it
using

stream = new StreamReader(filename);

Is it possible to assign a String instead of a file as the source of the
stream, and if yes, how?

Regards
Jesper.

Nov 17 '05 #1
6 4167
Jesper wrote:
Im using a code snippet that reads a text file from disc and processes it
using

stream = new StreamReader(filename);

Is it possible to assign a String instead of a file as the source of the
stream, and if yes, how?


Well, not for a StreamReader (easily) - but if all you need is a
*TextReader* (which is probable) just use StringReader instead.

Jon

Nov 17 '05 #2
Hello,
Yes you can, convert the string in to byte array using
System.Text.Encoding.ASCII.GetBytes method. Then pass those bytes to
MemoryStream class's constructor. You'll get and Stream object, but it
won't be of 'StreamReader' type.

HTH. Cheers.

Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Something like this:

string text = "hello streams";
StreamReader reader = new StreamReader (new MemoryStream (
System.Text.Encoding.ASCII.GetBytes (text )));

Ab.
http://joehacker.blogspot.com

"Maqsood Ahmed" <ma***********@gmail.com> wrote in message
news:uF**************@TK2MSFTNGP14.phx.gbl...
Hello,
Yes you can, convert the string in to byte array using
System.Text.Encoding.ASCII.GetBytes method. Then pass those bytes to
MemoryStream class's constructor. You'll get and Stream object, but it
won't be of 'StreamReader' type.

HTH. Cheers.

Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4
Maqsood Ahmed wrote:
Yes you can, convert the string in to byte array using
System.Text.Encoding.ASCII.GetBytes method. Then pass those bytes to
MemoryStream class's constructor. You'll get and Stream object, but it
won't be of 'StreamReader' type.


That's a really bad idea unless you've got solely ASCII characters.
You should at least use something which preserves Unicode characters -
eg Encoding.UTF8. (That also happens to be the default encoding used by
StreamReader...)

Jon

Nov 17 '05 #5
Hello,
Yes, it was just to *tell* :) it all depends upon the requirements of
the problem.

Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6
Maqsood Ahmed <ma***********@gmail.com> wrote:
Yes, it was just to *tell* :) it all depends upon the requirements of
the problem.


Unfortunately, in my experience if you specify a "solution" which
doesn't explain its own limitations, people tend to use it without
questioning whether or not it's actually fit for purpose :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: andrewcw | last post by:
OK I am half way there - I can manipulate the stream without the byte issue like this - but is this the way to push the new values back into the stream & write out the stream without resorting to...
4
by: Astronomically Confused | last post by:
using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; class HttpProcessor { private Socket s;
3
by: Arno | last post by:
Hi, I'm using TcpClient for communication between two PC running a small piece of software. The protocol used has been designed internally and is HTTP similar (command line, headers, body). A...
16
by: vvenk | last post by:
Hello: When I use either one to read a Text file, I get the same result. The length of the string that the file's content has been written into is the same. However, if the file is binary,...
2
by: Bryan Dickerson | last post by:
StreamReader says it is designed to read a stream of characters StringReader says it is designed to read a string TextReader says it is designed to read a sequential list of characters. I hate...
0
by: Al Wilkerson | last post by:
Hey, Has anyone ever got a "Unable to read data from transport connected" message after reading data from a streamreader composed of a networkstream. For example: Server TcpListener...
1
by: Arpan | last post by:
The contents of a text file are as follows: The Quick Brown Fox Jumped Over The Lazy Dog. Note that there isn't any space at the end of each of the 3 lines. Now when I do this:
4
by: somequestion | last post by:
Question 1. i am using StreamReader Class like this... string str = string.Empty; StreamReader sr = new StreamReader(fsIn,Encoding.Default) while ((str = sr.ReadLine()) != null) { // wanna get...
1
by: Sladan | last post by:
Im trying to read a xml-file with a StreamReader. For the moment I'm using the following code. streamReader = new StreamReader(stream, System.Text.Encoding.Default); string feedData =...
5
by: Dave Bootsma | last post by:
I have an application where I want to redraw a polygon from points I retrieve from a file. The file is read with streamreader line by line each line contains the points for each polygon. Below is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.