By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,616 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,616 IT Pros & Developers. It's quick & easy.

reading and writing a file

P: n/a
I wrote this code just to experiment with writing to and reading from a
file. It seems to work fine when writing, but when reading the file, it
only prints the filepath to the screen, not the file contents.

System.IO.StreamWriter file = new
System.IO.StreamWriter(@"C:\positions.txt");
System.IO.StringReader myFile = new
System.IO.StringReader(@"C:\positions.txt");

for (int i = 0; i < 3; i++)
{
string positions = Console.ReadLine();
file.WriteLine(positions);
}

Console.WriteLine("Will now read from file...");
Console.ReadLine();

for (int i = 0; i < 3; i++)
{
string getPos = myFile.ReadLine();
Console.WriteLine(getPos);
}

file.Close();
Console.ReadLine();
P.S. I followed the examples in C# Express. In other books I've read,
reading/writing of files usually involves a FileStream object too. Is
that not necessary?
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
John Salerno wrote:
System.IO.StringReader myFile = new
System.IO.StringReader(@"C:\positions.txt");


You're using a string reader. You need a stream reader. Also, you
haven't closed the file after writing to it.

-- Peter Gummer
Nov 17 '05 #2

P: n/a
Peter Gummer wrote:
John Salerno wrote:

System.IO.StringReader myFile = new
System.IO.StringReader(@"C:\positions.txt");

You're using a string reader. You need a stream reader. Also, you
haven't closed the file after writing to it.

-- Peter Gummer


Doh! I typed it in wrong! Thanks!

(embarrassed) :)
Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.