views:

228

answers:

5

Hello I am working on something, and I need to be able to be able to add text into a .txt file. Although I have this completed I have a small problem. I need to write the string in the middle of the file more or less. Example:

Hello my name is Brandon,
I hope someone can help, //I want the string under this line.
Thank you.

Hopefully someone can help with a solution.

Edit Alright thanks guys, I'll try to figure it out, probably going to just rewrite the whole file. Ok well the program I am making is related to the hosts file, and not everyone has the same hosts file, so I was wondering if there is a way to read their hosts file, and copy all of it, while adding the string to it?

+3  A: 

With regular files there's no way around it - you must read the text that follows the line you wish to append after, overwrite the file, and then append the original trailing text.

Think of files on disk as arrays - if you want to insert some items into the middle of an array, you need to shift all of the following items down to make room. The difference is that .NET offers convenience methods for arrays and Lists that make this easy to do. The file I/O APIs offer no such convenience methods, as far as I'm aware.

When you know in advance you need to insert in the middle of a file, it is often easier to simply write a new file with the altered content, and then perform a rename. If the file is small enough to read into memory, you can do this quite easily with some LINQ:

var allLines = File.ReadAllLines( filename ).ToList();
allLines.Insert( insertPos, "This is a new line..." );
File.WriteAllLines( filename, allLines.ToArray() );
LBushkin
+1  A: 

Check out File.ReadAllLines(). Probably the easiest way.

     string[] full_file = File.ReadAllLines("test.txt");
     List<string> l = new List<string>();
     l.AddRange(full_file);
     l.Insert(20, "Inserted String");
     File.WriteAllLines("test.txt", l.ToArray());
SwDevMan81
A: 

If you know the line index use readLine until you reach that line and write under it. If you know exactly he text of that line do the same but compare the text returned from readLine with the text that you are searching for and then write under that line.

Or you can search for the index of a specified string and writ after it using th escape sequence \n.

GxG
A: 

As others mentioned, there is no way around rewriting the file after the point of the newly inserted text if you must stick with a simple text file. Depending on your requirements, though, it might be possible to speed up the finding of location to start writing. If you knew that you needed to add data after line N, then you could maintain a separate "index" of the offsets of line numbers. That would allow you to seek directly to the necessary location to start reading/writing.

Mark Wilkins
+1  A: 

one of the trick is file transaction. first you read the file up to the line you want to add text but while reading keep saving the read lines in a separate file for example tmp.txt and then add your desired text to the tmp.txt (at the end of the file) after that continue the reading from the source file till the end. then replace the tmp.txt with the source file. at the end you got file with added text in the middle :)

ramtinova