Question:
C# Write to a new line in text file?
John
2012-03-23 15:03:13 UTC
I am writing a game in C# / XNA and I need to make the streamwriter write to the text file on the next available line. For example, it would be like this ("Username" + " " + "Score"), and this does write to the file, but it overwrites the previous text that was there. I simply want to make it add new entries on a new line. Thanks for any help you give :)
Four answers:
peteams
2012-03-23 15:49:02 UTC
If you just want to append username and score separate by a space to a new line at the end of a file called history.txt you can write:



File.AppendAllLines("history.txt", new string[] { username + " " + score });



If you want to use a StreamWriter you'd write something like:



using (var writer = File.AppendText("history.txt"))

{

writer.WriteLine(username + " " + score);

}
anonymous
2012-03-23 15:48:00 UTC
Matthew, that's not what he means. He means he needs to append text to the existing text in the file. Instead of writeline, use .appendtext
Matthew O'Neill
2012-03-23 15:18:48 UTC
use new line character "\n" ("Username \n Score"),

or use that .net class Environment.Newline ("Username" + Environment.Newline + "Score"),
anonymous
2017-01-16 14:55:23 UTC
#contain iostream #contain fstream #contain string using namespace std; ... ... ... string line, word_to_find; word_to_find = "in spite of"; ifstream fin ( filename ); whilst ( getline(fin,line) ) { if ( line.locate(word_to_find) != string::npos ) cout << line << endl; }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...