473,508 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Seek Function in C#

2 New Member
Hi All,

I was trying to overwrite the data in a file.
But seek function is not working as i expecting.
My code is here:

public void createfile()
{
FileStream fs = new FileStream(@"C:\Documents and Settings\MyText.txt", FileMode.Create, FileAccess.Write);
string text = "Welcome to DotNet Forum";
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(text);
sw.Write("File ended");
sw.Close();
fs.Close();
}
public void updatefile()
{
FileStream fs = new FileStream(@"C:\Documents and Settings\MyText.txt", FileMode.Open, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.Write(57);
sw.BaseStream.Seek(8, SeekOrigin.Begin);
sw.Write(22);
sw.Close();
fs.Close();
}

According to my expectation it should return "57lcome 22 DotNet Forum" but it is returning "Welcome 5722otNet Forum";

Seek function has called before sw.Write().
Can anyone help me on this?

Thanks
Vineeta
Sep 30 '08 #1
4 3195
Plater
7,872 Recognized Expert Expert
Oh delayed writes you are a fical mistress.
It happens because the data hasn't actually been written to the underlying stream yet by the time you call the seek function.
(Also your example you forgot to put quotes around your strings)

Expand|Select|Wrap|Line Numbers
  1. public void updatefile()
  2. {
  3.    FileStream fs = new FileStream(@"C:\MyText.txt", FileMode.Open, FileAccess.ReadWrite);
  4.    StreamWriter sw = new StreamWriter(fs);
  5.    sw.Write("57");
  6.    sw.Flush();
  7.    sw.BaseStream.Seek(8, SeekOrigin.Begin);
  8.    sw.Write("22");
  9.    sw.Close();
  10.    fs.Close();
  11. }
  12.  
Sep 30 '08 #2
vineeta
2 New Member
Thanks Plater,

actually write is a overloaded function of StreamWriter and it takes everything as text so quotes are not needed. I have added some delay also

sw.Write("57");
Thread.Sleep(10000);
sw.BaseStream.Seek(8, SeekOrigin.Begin);
sw.Write("22");

Still it is behaving as earlier

-Vineeta
Oct 1 '08 #3
r035198x
13,262 MVP
Try calling streamreader.DiscardBufferedData(); between the writes.
Oct 1 '08 #4
Plater
7,872 Recognized Expert Expert
You shouldn't need the delay if you use flush. There is no garuntee that the delay will *always* be long enough to do the write. It could be waaay to long, or it could be just a bit too short, depending on how much data is to write.
Using the flush forces the application to wait until the data is written, and only the amount of time it takes to write the data.
Oct 1 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4586
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
1
4205
by: Waitman Gobble | last post by:
Hello, I am new to Python. I am having trouble with zipfile.py. On a Linux machine with python 2.4.2 I have trouble opening a zipfile. Python is complaining about the bit where it does a...
11
35697
by: Tiger | last post by:
We can use seek() in the FileStream class,as we know. But I found that seek() is not work correctly in StreamReader. Who can tell me how to use seek() correctly in StreamReader? thanks a lot! I...
3
4960
by: Mark Denardo | last post by:
Does anyone have any good VB.NET example code that shows how to use the NOTIFY option using the mciSendString API and then handle the return value. The only examples I can find show the VB way...
33
11276
by: Kevin Brammer | last post by:
I'm trying to use seek to check for the existence of a record before saving, so there are no duplicate entries (is there another way?). I have a "groups" table, which has GroupID Island...
59
7433
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when...
3
6587
by: Jonnh | last post by:
help with use the methods recordset and seek in access because en excel with function database, i know but en access i need seek to table or form, thanks
0
1384
by: the_kid | last post by:
I'm trying to build a Indonesian-english dictionary/translator aplication. I have an unbound form (Form1) with two text field (text1) & (text2) and two command button (command1) and (command2)....
3
10598
by: Michal | last post by:
Does any one know how to create a function that works similar as "Goal Seek" in Excel? For example: var a=8; var b=6; function c(a,b) { return a*b; }
0
7323
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
7380
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...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.