473,750 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random access file IO - what is wrong with this code?

Problem code:

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.IO;
using System.Diagnost ics;

namespace Streams {
class Program {
static void Main(string[] args) {
const string file = @"C:\Temp\file. txt";

if (File.Exists(fi le))
File.Delete(fil e);

using (StreamWriter sw = new StreamWriter(fi le)) {
sw.WriteLine("L ine 1");
sw.WriteLine("L ine 2");
sw.WriteLine("L ine 3");

FileStream fs = sw.BaseStream as FileStream;
Debug.Assert(fs != null);

fs.Seek(0, SeekOrigin.Begi n);
sw.WriteLine("L ine A");
}
}
}
}

Expected result: a file with 3 lines
Line A
Line 2
Line 3

Actual result: a file with 4 lines
Line 1
Line 2
Line 3
Line A
The Seek() is being ignored.

Obviously I am doing something wrong - any hints guys?!
--
Phil
Aug 16 '06 #1
2 1504
Hi,

..Flush() makes it work:

sw.WriteLine("L ine 1");
sw.WriteLine("L ine 2");
sw.WriteLine("L ine 3");
sw.Flush();
FileStream fs = sw.BaseStream as FileStream;
Debug.Assert(fs != null);
fs.Seek(0, SeekOrigin.Begi n);
sw.WriteLine("L ine A");
sw.Flush();

not sure it's the best solution...

Mike
http://www.seeknsnatch.com

"Phil" <fo*@foo.comwro te in message
news:a1******** *************** *********@4ax.c om...
Problem code:

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.IO;
using System.Diagnost ics;

namespace Streams {
class Program {
static void Main(string[] args) {
const string file = @"C:\Temp\file. txt";

if (File.Exists(fi le))
File.Delete(fil e);

using (StreamWriter sw = new StreamWriter(fi le)) {
sw.WriteLine("L ine 1");
sw.WriteLine("L ine 2");
sw.WriteLine("L ine 3");

FileStream fs = sw.BaseStream as FileStream;
Debug.Assert(fs != null);

fs.Seek(0, SeekOrigin.Begi n);
sw.WriteLine("L ine A");
}
}
}
}

Expected result: a file with 3 lines
Line A
Line 2
Line 3

Actual result: a file with 4 lines
Line 1
Line 2
Line 3
Line A
The Seek() is being ignored.

Obviously I am doing something wrong - any hints guys?!
--
Phil

Aug 16 '06 #2
On Thu, 17 Aug 2006 00:10:15 +0200, "Michael" <in**@seeknsnat ch.com>
wrote:
>Hi,

.Flush() makes it work:

sw.WriteLine(" Line 1");
sw.WriteLine(" Line 2");
sw.WriteLine(" Line 3");
sw.Flush();
FileStream fs = sw.BaseStream as FileStream;
Debug.Assert(f s != null);
fs.Seek(0, SeekOrigin.Begi n);
sw.WriteLine(" Line A");
sw.Flush();

not sure it's the best solution...

Mike
http://www.seeknsnatch.com
Thanks Michael I will give that a shot.
--
Phil
Aug 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5172
by: Robert | last post by:
I have the XML file which has a structure similar to one I pasted at the end of this post. As you can see many elements are using different id attributes. For example element Window uses WinID attribute as ID, the ViewData uses ViewID attribute as an ID and so on. I need to read this file without assuming what is the order of the elements, in other words I can't read it sequentially. The reason for this requirement is that whole WindowsInfo...
5
2949
by: Raterus | last post by:
I'm just throwing this error out for my sanity, I've seen posts about this, but never solutions. I'm using VS.NET 2003, Framework 1.1, and I'm getting a random error about every 1 out of 10 times I try to start to run/debug a project (through Visual Studio) Server Error in '/Intranet' Application. -------------------------------------------------------------------------------- Compilation Error Description: An error occurred during...
3
2359
by: VB.NET | last post by:
I'm using a mysql database and connecting my vb.net program to the DB over a network connection. i would like to bring this data over to a vb.net random access file. does anyone know how to convert my DB into a binary file? and how to convert my code to use a file instead of the db?
3
3025
by: Bruce | last post by:
I am building a WinForms app that uses Web Services access to a server for most of its data input/output, but I also need to persist some of its data to the local disk (basically as a cache of some of the Web Services data) in XML format. Since the size of the XML local store could be rather large, I'd prefer to have a random access mechanism for reading and writing to it. It seems that XMLReader /XMLWriter are sequentially fast,...
14
9403
by: DataSmash | last post by:
Hi, When I import the random module at the python interpreter, it works fine: >>> import random >>> x = random.randint(1,55) >>> print x 14 >>> BUT, when I put the same code in a python script:
16
7188
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain unchanged). How can I do that in Python? Claudio Grondi
48
4266
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) * -1 Set oConn=Server.CreateObject("ADODB.Connection") Set oRS=Server.CreateObject("ADODB.recordset") oConn.Provider="Microsoft.Jet.OLEDB.4.0" oConn.Open Server.MapPath("temp.mdb")
39
2469
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and MyClass from module2. module2 does not import random. module1 sets a seed like this::
5
1336
by: LayneMitch via WebmasterKB.com | last post by:
Code is suppose to choose a random name using 'event listeners'. The reference file is a file downloaded from the book's publisher "Sitepoint". I doubt that there is anything wrong with the file itself. There has to be something wrong with the code or my referencing of the file (the file is in the same directory as the code). Not sure, but here's the code: <html><head><title>Prob.4</title> <style type="text/css"> #textfield...
0
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
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
9575
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
9338
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
8260
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
6803
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
6080
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.