Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello,
I have text file with writen lines. I want to open it and after that lines write new new line and don't overwrite old lines.

I've tried FileStream with StreamWriter with this code:
C#
sw.WriteLine();
sw.WriteLine();
sw.WriteLine();
sw.WriteLine("{0}", value);


but this will only make blank lines before writen lines.

Please how to do it? Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 26-Feb-12 13:32pm    
I'm just curious: what made your thinking that WriteLine can do anything except writing, like skipping to the next line without modifying anything? This like just write one or two (depending on platform, as I put in my answer to your previous question) end-of-line characters. Write is write... If you are trying to program using the principle "kick the TV set until it shows a picture" you won't go far. :-)
--SA

You asked about a similar problem only the other day - really you ought to research this topic properly not keep asking questions on it. However, I will answer your question again this time...

Since you open a stream to the file and the stream starts by pointing to the start of the file, calling the WriteLine() method will just keep adding stuff to the beginning. Keeping a file open while doing manipulation of it (especially when it is just a text file) is normally a bad thing to do. Therefore, use a StreamReader object to open the file and use the ReadToEnd() method to read in all the text from that file. Store this in a variable wherever, it doesn't matter really. Anyway, remeber to call Close on your StreamReader object. Then manipulate (i.e. add new lines) to the string as I showed you in my answer to your last question. Finally, use a StreamWriter to WriteAlltext your variable containing the text to the file - (As someone also showed in your last question). Remember to make it overwrite existing content since what you had was a copy of the file in the first place. Remember also to call Close on your StreamWriter so that it releases the file.

Hope this helps,
Ed

Edit: See Solution 3 of your last question for a good example that uses the File class - this is better than StreamWriters/Readers!
 
Share this answer
 
v2
Comments
LosEagle 26-Feb-12 13:16pm    
Sorry didn't know that this problem will have similar solution like last one because I didn't even though that I will need to use both StreamReader and StreamWriter just to add lines. Thanks for help I will give it try.
Ed Nutting 26-Feb-12 13:22pm    
You don't even need to use those - Solution 3 of your last question gave you exactly what you wanted!

string filepath = ...;
string text = File.ReadAllText(filepath);
//Use my solution to add lines
AppendNewLine(text);
AppendNewLine(text);
AppendNewLine(text);
//Then write the text back to the file again.
File.WriteAllText(filepath, text);


Hope you can see how easy this is,
Ed
Sergey Alexandrovich Kryukov 26-Feb-12 13:39pm    
I voted 4, because you missed important thing: append.
--SA
Ed Nutting 26-Feb-12 13:43pm    
I do not understand what you think I missed? The AppendNewLine method (as you have seen) is in my previous answer and can easily be found by others. Other than that, what appears to be missing? Or is reviewing me own code failing me again...?

Edit: Okay have seen your solution now (after Chrome refreshed properly) - I see what you mean but for my solution (see comment above) the OP doesn't want the Append mode, OP wants overwrite mode - I fear your new information may confuse the OP, given the OP's previous "display of ability". However, your point is very relevant and valid, my 4 (since it doesn't answer the full original question) - between us we have.
Sergey Alexandrovich Kryukov 26-Feb-12 13:54pm    
Append option or file/stream mode.
--SA
I'm not sure the answer by Ed would be enough.

When working with stream, you should pay attention for the mode of working with existing files. Only when you open a file or stream, you can define if you are going to override the content of the file or append to it.

For example StreamWriter has different constructors:
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^].

Some constructors have append parameter:
http://msdn.microsoft.com/en-us/library/36b035cb.aspx[^],
http://msdn.microsoft.com/en-us/library/f5f5x7kt.aspx[^],
http://msdn.microsoft.com/en-us/library/0wf7ab94.aspx[^].

Same thing with stream and file classes. Just pay attention for the options or "file mode" when you create an instance.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900