472,370 Members | 2,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,370 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 4092
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.