C
C#•2mo ago
LostAngel

text file management

hey, so i'm trying to use the streamwriter function to write in a text file, but it always replace the text it's in it, how do i add lines without deleting the preceding texts?
54 Replies
jcotton42
jcotton42•2mo ago
how are you opening the file?
LostAngel
LostAngel•2mo ago
i'm opening it with the streamwriter function, like that
StreamWriter sw = new StreamWriter($"./datas/Players/{e.Author.Id}/Historique.txt");
sw.WriteLine($"the main text");
sw.Flush();
sw.Close();
StreamWriter sw = new StreamWriter($"./datas/Players/{e.Author.Id}/Historique.txt");
sw.WriteLine($"the main text");
sw.Flush();
sw.Close();
and i'm doing the same thing when i want to add a new text line
jcotton42
jcotton42•2mo ago
use AppendText to open the file
jcotton42
jcotton42•2mo ago
File.AppendText(String) Method (System.IO)
Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.
jcotton42
jcotton42•2mo ago
so
using var writer = File.AppendText("path");
sw.WriteLine("the main text"); // no need for $ when you're not interpolating
// because we used 'using' above, the file is closed at the end of the scope
using var writer = File.AppendText("path");
sw.WriteLine("the main text"); // no need for $ when you're not interpolating
// because we used 'using' above, the file is closed at the end of the scope
LostAngel
LostAngel•2mo ago
and it can add a new line without deleting the existing text in the file?
jcotton42
jcotton42•2mo ago
it's called AppendText for a reason 🙂 from the summary on the docs
Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.
LostAngel
LostAngel•2mo ago
okay, i'll try that then uh, now it deletes the second line-
jcotton42
jcotton42•2mo ago
Show your code
LostAngel
LostAngel•2mo ago
No description
LostAngel
LostAngel•2mo ago
this is how i did it @jcotton42
jcotton42
jcotton42•2mo ago
is that the only place you're writing? also, how big is the enclosing block? since the file won't be closed and written until the end of that block
LostAngel
LostAngel•2mo ago
for now there is only two places i use that writing method the enclosing block is just a bit down, 2 rows
LostAngel
LostAngel•2mo ago
No description
LostAngel
LostAngel•2mo ago
@I can't be used on production
Memw
Memw•2mo ago
ah let me read
Memw
Memw•2mo ago
Memw
Memw•2mo ago
@LostAngel is this what you wanted?
LostAngel
LostAngel•2mo ago
yeah, and i want it to add more and more textes without having the second text to be overwriten
Memw
Memw•2mo ago
in the video I sent that doesn't happen you can keep appending text to the file
using FileStream file = File.Open("test.txt", FileMode.Append);
file.Write("This is a test!\n"u8);
using FileStream file = File.Open("test.txt", FileMode.Append);
file.Write("This is a test!\n"u8);
LostAngel
LostAngel•2mo ago
you don't need the line in writer.WriteLine ?
Memw
Memw•2mo ago
the u8 is the same as doing Encoding.UTF8.GetBytes("This is a test!\n") That method is not part of a FileStream that's part of a StreamWriter
LostAngel
LostAngel•2mo ago
yeah, it's part of StreamWriter
Memw
Memw•2mo ago
Simply use the File.Open method with the FileMode.Append option and you should have no problem
LostAngel
LostAngel•2mo ago
let me test that then ^^'
Memw
Memw•2mo ago
Did it work?
LostAngel
LostAngel•2mo ago
i'm having a code error when typing the code
LostAngel
LostAngel•2mo ago
(sorry it's in french)
No description
Memw
Memw•2mo ago
add u8 at the end of the string $".."u8
LostAngel
LostAngel•2mo ago
No description
Memw
Memw•2mo ago
without , .
LostAngel
LostAngel•2mo ago
No description
Memw
Memw•2mo ago
ah right it's interpolated, interpolated strings can't do that do Encoding.UTF8.GetBytes("..")
LostAngel
LostAngel•2mo ago
No description
LostAngel
LostAngel•2mo ago
like that?
Memw
Memw•2mo ago
yeah
LostAngel
LostAngel•2mo ago
i'll test that it still removes the preceding 2nd text
Memw
Memw•2mo ago
ok, so basically, if that happens for you (weird asf) I'd say that you could just append two \n\n at the end, so it deletes an empty line and adds a next one
LostAngel
LostAngel•2mo ago
file content before : History of angethemessager : - 754 : name ★ (ressource) file content after : History of angethemessager : - 755 : name ★ (ressource)
Memw
Memw•2mo ago
that might be a logic error in another part of the program tho something that deletes the file somewhere else, because AppendText should work and aswell Write from FileStream you maybe opening the file in writemode somewhere else
LostAngel
LostAngel•2mo ago
No description
Memw
Memw•2mo ago
ahh
LostAngel
LostAngel•2mo ago
this is the only second time i use the file management there
Memw
Memw•2mo ago
you are creating the file and reopening it do this wait
LostAngel
LostAngel•2mo ago
(it's in another .cs file)
Memw
Memw•2mo ago
AHH first of all
Memw
Memw•2mo ago
name missmatch
No description
Memw
Memw•2mo ago
Historigue and Historique
LostAngel
LostAngel•2mo ago
oh 🤦
Memw
Memw•2mo ago
so it recreates the file because Historigue never exists and also you could improve efficiency by doing this
LostAngel
LostAngel•2mo ago
well yeah it's that one error that removed all the text each time 🤦
Memw
Memw•2mo ago
string historiqueChemin = $"./datas/Players/{e.Author.Id}/Historique.txt";

if (!File.Exists(historiqueChemin))
{
using FileStream file = File.Create(historiqueChemin);
file.Write(Encoding.UTF8.GetBytes($"Historique des pulls du joueur {p.PlayerName} :"));
}
string historiqueChemin = $"./datas/Players/{e.Author.Id}/Historique.txt";

if (!File.Exists(historiqueChemin))
{
using FileStream file = File.Create(historiqueChemin);
file.Write(Encoding.UTF8.GetBytes($"Historique des pulls du joueur {p.PlayerName} :"));
}
with this you don't call the string interpolation handler two times while checking the file and creating it, and you use the already opened FileStream so you don't close one and open another for no reason @LostAngel
LostAngel
LostAngel•2mo ago
alrighty thanks ^^
Want results from more Discord servers?
Add your server
More Posts