tags:

views:

102

answers:

3

I have a web app that I am developing at work. I need to be able to take input data and append a text file after (x) number of lines.

My web app is using asp.net with c#

Can anyone help me please?

+1  A: 

There's no way of "inserting" into a file in general - or of going to a specific line, without reading all the others, unless they're of a fixed size (in bytes).

Normally the approach would be something like:

  • Start writing a new file
  • Open the existing file
  • Copy the first x lines from the old file to the new one
  • Write the new line
  • Copy the remaining lines from the old file to the new one
  • Move the old one to a backup file
  • Move the new file to the old name
  • Delete the backup file

(This ensures that at any one point there's at least the old file in some form. You can make it slightly simpler if you just delete the old file and then move the new one into place.)

Don't forget to ensure this is synchronized appropriately - you don't want to have two copies of this algorithm running at the same time...

EDIT: If you've got XML files, then I'd suggest usually just loading it into the DOM (e.g. with LINQ to XML), making the change, and then saving it out again. Don't treat it just like an unstructured text file.

You could potentially make this more efficient using XmlReader and XmlWriter - but you're certainly going to have to read the whole original file and write out the new file. Have you benchmarked simple code and found it too slow? How often are you doing this? How big are the files?

Jon Skeet
This is a decent wait to go. Anyone else know of an better/faster way?This is for modifying an xml file windows play lists (wpl)I'm new to working with XML
bret
@bret: I've edit my answer now you've mentioned it's XML.
Jon Skeet
Now that Jon's smoked out the XML format, perhaps Bret should look at storing the XML in SQL Server. Start with: http://stackoverflow.com/questions/966441/xml-query-in-sql-server-2008
ebpower
Jon, thank you for your quick replies. I have started looking at XmlReader and xmlWriter. The only thing i don't know how to do is add to the specific node.the structure is a bit funny:<?xml version="1.0" encoding="utf-8"?><smil> <head> <meta /> <title> funny </title> </head> <body> <seq> <media src="filename" /> </seq> </body></smil>
bret
@bret: I don't see what's funny about that really... but as I say, I'd load it all into something like `XDocument` and manipulate it that way, personally. It'll be a lot simpler than using `XmlReader` and `XmlWriter`.
Jon Skeet
@Jon: Looking at a way to do it with XDocument now, however I keep getting: "System.NullReferenceException: Object reference not set to an instance of an object."with code:document.Root.Element("smil").Element("body").Element("seq").Add(new XElement("media", new XAttribute("src",path)));is there a more efficient way, or a way that works?Thanks for all of your help
bret
A: 

I would suggest finding another strategy, specifically a relational database management system. A text file lives on the file system and does not support concurrent access like a good (read:not Access) database. A web application does support concurrent requests. Once you have more than one user working at the same time, your app will experience IO Exceptions.

matt-dot-net
Users are all pulling from seperate files, it is based on windows authentication. When the user hits the site, the information is loaded from a folder created specifically for them using their Pre-W2K logon name.
bret
One user could still issue concurrent requests with their browser. In fact you do it every time you load a page with more than one image. The more poignant case though is when your Windows Authenticated, unique user, double clicks a submit button causing concurrent POSTs to your server. I'm just suggesting that file system data (that's not read only) is maybe not the best practice.
matt-dot-net
Duely noted. Thanks for the expertise and advice. I should clarify that I'm not really a programmer or web developer. I am active duty Air Force and am working ont his project because there is no one else available.
bret
A: 
bret