473,660 Members | 2,468 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.Writ e);
string text = "Welcome to DotNet Forum";
StreamWriter sw = new StreamWriter(fs );
sw.WriteLine(te xt);
sw.Write("File ended");
sw.Close();
fs.Close();
}
public void updatefile()
{
FileStream fs = new FileStream(@"C: \Documents and Settings\MyText .txt", FileMode.Open, FileAccess.Read Write);
StreamWriter sw = new StreamWriter(fs );
sw.Write(57);
sw.BaseStream.S eek(8, SeekOrigin.Begi n);
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 3214
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(10 000);
sw.BaseStream.S eek(8, SeekOrigin.Begi n);
sw.Write("22");

Still it is behaving as earlier

-Vineeta
Oct 1 '08 #3
r035198x
13,262 MVP
Try calling streamreader.Di scardBufferedDa ta(); 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
4599
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 "file.tell() and file.seek() in order to navigate through a file in order to print specific lines (for example, every 5th line). Allow me to illustrate by example:
1
4209
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 seek(-22,2). Looks to me like zipfile.py is trying to come back 22 bytes from the end of file. # python
11
35761
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 use the seek() like this: StreamReader r; ....... r.BaseStream.seek(.....);
3
4970
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 using <Form>.hWnd My program basically needs to start a media file and then be informed when it has finished. Right now I have to set up a polling thread to check the status of the playing media file, which works, but causes other parts of my...
33
11298
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 GroupName My code right now is giving me a "type mismatch" error, and this is what it looks like:
59
7491
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 the recordset opens a table. When I write Set rst = db.OpenRecordset("MyTable",dbOpenTable, dbReadOnly) I get an error. I believe it's invalid operation or invalid parameter, I'm
3
6626
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
1392
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). My aim is to type sentences in Indonesian in text1 and translate into english into text2 trough steps as follows: 1. split the sentences in text1 and store it into tabel T_TEMP (i've succeded with this )
3
10622
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
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4176
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
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.