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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StreamWriter .Write method 1

Status
Not open for further replies.

ccampbell

Programmer
Aug 16, 2001
201
US
I am having problems writing to a txt file using the StreamWriter class. I can get the data to write tot he file, but I need to be able to place that data at certain places within the file. For instance val1 needs to occupy the first 12 slots in the file, val2 needs to occupy the second 3 slots and so on and so forth. I have tried to use BaseStream.Seek to do this and I have also tried to use the StreamWriter.Write function to do this. When I hover over the StreamWriter.Write function it tells me void StreamWriter.Write(string value) (+20 overloads). Does anyone know how to write values to a file and specify what place in the file the value should take. In C++ I have code that will seek to a position in the file and then write the data. I need to do the same actions with C#. Your help is much appreciated.
 
what you need is to create a FileStream, make it seek the position you need then write your StreamWriter to that FileStream. a very basic example is:
Code:
	FileStream fs = new FileStream("", FileMode.Append);
	// go after the first 25 bytes
        fs.Seek(25, SeekOrigin.Begin);
	StreamWriter sw = new StreamWriter(fs);
	sw.Write("string to write");
 
where you modify the filestream to open the file you need
 
Excellent,exactly what I needed! On another subject, do you know how to view the next node of an XML file? Right now I am using an XmlTextReader object and a switch/case statement to determine the nodes. When I am in some case statements I need to check the data value of the next node before breaking and moving to the next node. Any ideas on what method will allow me to do this? THanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top