CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2010
    Posts
    36

    Best way to insert new data into files?

    I need a bit of advice. I want to insert new data into an existing file but keep the current information that is in the file intact. When I used to do this in Perl I did it very slowly by reading line by line into a large varible until I found where I wanted to put the new info in, then appeneded the info to the varible and finished reading the file to the varible. Well needless to say that took a lot of memory and CPU time. While I have not looked much into it for C# I have to head off to my 9 hour shift of work right now and though I would pose the question to see what you guys think while I do a bit of searching when at work. So what would you recomemnd for the easiest and fastest way to insert information into an existing file?

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Best way to insert new data into files?

    What is the format of the file? It is your own, or another? Can you use any driver? And not why to use a database? It seems exactly like job for it.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Best way to insert new data into files?

    I would go with boudino's recommendation. By the look of it you would want to place the new information into a specific place in the file (i.e. amongst existing data). I think this is a complicated way of doing it. If you are concerned about order go for a database storage system. If for some reason you want to avoid the database option go for XML format. The XML option won't be very quick (if you don't have lots of data you wouldn't notice any difference) as you would have to load the whole file into memory, do the update and then save the file. If you were not concerned about the order you could just open the file and append the data at the end...

  4. #4
    Join Date
    Feb 2010
    Posts
    36

    Re: Best way to insert new data into files?

    It would be .aspx pages with no code behind. What I am doing is making a CMS for a website I run. There are pre-created files that would just be copied to new locations for information that is stored in the database but some other files I just saw it as a waste of DB space to store as they would be always static information. One example would be for a PS2 game the character discriptions. Once written that information would only change if there was a type-o so why waste the DB space and overhead to dynamicly generate that page every time it was loaded. Why not just create a static .aspx page to serve that information up?

    Although now that I think of it I can change the code idea I had in my head to instaed of reading a template file and inserting the information into the template I can move the header and footer of the template into the c# varibles and generate the hole file from scratch each time that script is called.

  5. #5
    Join Date
    Dec 2009
    Posts
    22

    Re: Best way to insert new data into files?

    Refactoring is great

  6. #6
    Join Date
    Feb 2010
    Posts
    36

    Re: Best way to insert new data into files?

    Quote Originally Posted by jonlist View Post
    Refactoring is great
    What do you mean refactoring?

  7. #7
    Join Date
    Dec 2009
    Posts
    22

    Re: Best way to insert new data into files?

    Sorry, i just meant, that it is great when you encounter a problem that makes you realize that the structure of your program can be improved

  8. #8
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Best way to insert new data into files?

    and what's the relavance of the revactoring to the OP's question?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured