Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
When by Using the Stream Reader if we read the File Line By Line.
 
Now If I wants to Write The File using StreamWriter At Some Specific Position Then How I will Come to Know about That When I was Present at that Specific Position.
 
Can Anyone Please Share The Explanation with an Example/Code Snippet ..?
Posted 17-Jan-13 9:03am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use the Seek method of the BaseStream property of StreamWriter class to jump to the location.
 
sample code
FileStream stream = new FileStream(@"filename.txt",FileMode.Open,FileAccess.ReadWrite);
StreamWriter writer = new StreamWriter(stream);
 
writer.BaseStream.Seek(10000, SeekOrigin.Begin); // where 10000 is the offset to jump
writer.WriteLine("new Test starts here");
Check MSDN-Seek[^] and BaseStream[^] links for more details
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 17-Jan-13 16:56pm
   
Correct, a 5.
—SA
jibesh at 17-Jan-13 16:58pm
   
Thank you SA
AyushJain1890 at 23-Jan-13 13:32pm
   
Also The Small Addition to the above code.
writer.Close();
writer.Dispose();
 
You Need To Close The writer because The Data is in the RAM before closing the writer, Once the writer is closed the data is written in the file as the Data is Transferred from RAM to main memory
jibesh at 23-Jan-13 13:34pm
   
Yes, thats Obvious. once should read the MSDN Remarks section before start using the method ;)
AyushJain1890 at 23-Jan-13 13:40pm
   
Thanks Jibesh For Your Help.
jibesh at 23-Jan-13 13:41pm
   
Glad to hear that. Cheers.
AyushJain1890 at 23-Jan-13 13:41pm
   
Also one More Question..
 
If you are reading any file and You have Completed reading up-to a Specific Line now You want to Know the offset i.e. The Position Up to Which you Have Completed Reading.

Any Sample Code Will Help.
jibesh at 23-Jan-13 13:44pm
   
You can use the Position property of the BaseStream to know current position in the stream.
AyushJain1890 at 23-Jan-13 14:00pm
   
Thanks..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 235
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100