1

I am trying to append a line to a text file but by removing the enclosing bracket first.

Below is how my text file data format looks like

{
"1455494402": 8,
 "1456272000": 2,
 "1456358400": 1}

Now when I append the text file data should look like this

{
"1455494402": 8,
 "1456272000": 2,
 "1456358400": 1,
 "1454716800": 1,
 "1454803200": 4,
 "1454889600": 7,
 "1458345600": 17,
 "1458518400": 1 }

There are two options to do this, I think,

  • By overwriting the whole file with new data (burden right? Performance hit)
  • Or By just appending the latest data(seems fast but not possible without removing last bracket)

First option is not so smart, I think.

Second option is cool but how do I remove the last bracket before appending the latest data, can't I just replace } with new data ?

So far my research has taught me that writing to the file again is the only better option, do you also think so? can't just append (in the sense remove bracket and append)

EDIT: Please do not consider this as duplicate, i am willing to know if that second option possible or not ? However I know i can do with first option mentioned in the details above

  • 1
    You can open a file for writing, `Seek` to just before the end, then write. But yes, JSON is probably wrong for this kind of thing. – Amadan Apr 08 '16 at 07:51
  • 1
    How big is the file going to get? To manipulate the file like that is going to be quite complicated. It's far easier to read the file into a data structure, manipulate that structure (append data etc.) and then write the new data out again. – ChrisF Apr 08 '16 at 07:52
  • That is basically a Json file. Parse it amend the object [turn it back into a string](http://stackoverflow.com/questions/16921652/how-to-write-a-json-file-in-c/16921677#16921677) – Liam Apr 08 '16 at 07:53
  • Possible duplicate of [Add multiple items in JSON array to object in C# using Json.net](http://stackoverflow.com/questions/4752429/add-multiple-items-in-json-array-to-object-in-c-sharp-using-json-net) – Liam Apr 08 '16 at 07:54
  • @Liam no, I am saving this to a .txt file and not to .json file, and I am not interested in JSON parse, i do not know much about it, it is not about format, it is about replacing string in text file without overwriting the whole file again – santhoshkumar B Apr 08 '16 at 07:59
  • Do you really need JSON? Can this just be a file where each line is in format: `"1458345600": 17,`, i.e. no brackets – weston Apr 08 '16 at 08:00
  • @Amadan correct, i first used JSON for this and switched back to stringbuilder which eased the formatting of data that my js actually wanted – santhoshkumar B Apr 08 '16 at 08:01
  • Is the question about replacing text in middle of file, or at end of file? – weston Apr 08 '16 at 08:01
  • @ChrisF Hi, I am trying to get the date values and respective counts for each date. thus for next one year i can safely assume there will be 300+ entries into this file, multiplied by years as we move on, thus i feel overwriting will be performance hit – santhoshkumar B Apr 08 '16 at 08:02
  • @weston hi , this is not at all about json, i am saving all of this in a text file named values.txt ...yes this is question of replacing a character like } with the new data like this ,"1459900800": 2 ,"1459987200": 1} – santhoshkumar B Apr 08 '16 at 08:04
  • At **end** of file? or **anywhere** in file? – weston Apr 08 '16 at 08:05
  • @weston at the end of file, you can notice that the file data ends with } hence i wish to remove the } and insert my new data like this ,"1459900800": 2 ,"1459987200": 1} – santhoshkumar B Apr 08 '16 at 08:07
  • 1
    Json is a format not a file type. You can store json in any file format. That file format is JavaScriptObjectNotation (JSON). It'll be consideribly easier to deal with if you treat it as such instead of an unstructured file. – Liam Apr 08 '16 at 08:12
  • @Liam upvoted, thanks, i dint knew that..fine, however i think seek is the solution for this – santhoshkumar B Apr 08 '16 at 08:13
  • 2
    @santhoshkumarB For only 300 entries, even multiplied by years, you're overthinking this. The performance hit from overwriting is negligible. – Martheen Apr 08 '16 at 08:24
  • @Martheen umm upvoted, Thank you for that sir – santhoshkumar B Apr 08 '16 at 08:29

1 Answers1

3

Assuming that:

  • You know that there will be a curly bracket (or any other character that is encoded as a single byte) at the end of the file,
  • The file is written as UTF8

then you can overwrite the last character as follows:

string filename = "test.txt";
File.WriteAllText(filename, "{One\nTwo\nThree}"); // Note curly brace at the end.

using (var file = new StreamWriter(File.OpenWrite(filename)))
{
    file.BaseStream.Position = file.BaseStream.Length - 1;
    file.Write("\nfour}"); // New line at end, previous brace is replaced.
}

This is very fragile, however. If the last character happens to be one that is encoded in more than one byte, then this will not work.

It is likely that it's not worth you taking the chance unless the files are very large and you have made timings that indicate it is worth introducing such brittle code to speed it up.

Note that this code can also be modified to work with ASCII or ANSI files by changing the encoding passed to the StreamWriter() constructor.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Thank you for the answer and time, Yes it is precisely a curly bracket at the end and that wont change in any case as i am appending new data with curly brackets at the end. Your solution sounds similar to the seek method and i will test both now, Thank you once again Matthew – santhoshkumar B Apr 08 '16 at 08:33
  • 1
    @santhoshkumarB Yes, setting the `.Length` property of a file stream is the same as seeking. – Matthew Watson Apr 08 '16 at 08:46