C
C#•7mo ago
DarkAngel

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•7mo ago
how are you opening the file?
DarkAngel
DarkAngelOP•7mo 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•7mo ago
use AppendText to open the file
jcotton42
jcotton42•7mo 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•7mo 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
DarkAngel
DarkAngelOP•7mo ago
and it can add a new line without deleting the existing text in the file?
jcotton42
jcotton42•7mo 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.
DarkAngel
DarkAngelOP•7mo ago
okay, i'll try that then uh, now it deletes the second line-
jcotton42
jcotton42•7mo ago
Show your code
DarkAngel
DarkAngelOP•7mo ago
No description
DarkAngel
DarkAngelOP•7mo ago
this is how i did it @jcotton42
jcotton42
jcotton42•7mo 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
DarkAngel
DarkAngelOP•7mo ago
for now there is only two places i use that writing method the enclosing block is just a bit down, 2 rows
DarkAngel
DarkAngelOP•7mo ago
No description
DarkAngel
DarkAngelOP•7mo ago
@I can't be used on production
Memw
Memw•7mo ago
ah let me read
Memw
Memw•7mo ago
Memw
Memw•7mo ago
@LostAngel is this what you wanted?
DarkAngel
DarkAngelOP•7mo ago
yeah, and i want it to add more and more textes without having the second text to be overwriten
Memw
Memw•7mo 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);
DarkAngel
DarkAngelOP•7mo ago
you don't need the line in writer.WriteLine ?
Memw
Memw•7mo 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
DarkAngel
DarkAngelOP•7mo ago
yeah, it's part of StreamWriter
Memw
Memw•7mo ago
Simply use the File.Open method with the FileMode.Append option and you should have no problem
DarkAngel
DarkAngelOP•7mo ago
let me test that then ^^'
Memw
Memw•7mo ago
Did it work?
DarkAngel
DarkAngelOP•7mo ago
i'm having a code error when typing the code
DarkAngel
DarkAngelOP•7mo ago
(sorry it's in french)
No description
Memw
Memw•7mo ago
add u8 at the end of the string $".."u8
DarkAngel
DarkAngelOP•7mo ago
No description
Memw
Memw•7mo ago
without , .
DarkAngel
DarkAngelOP•7mo ago
No description
Memw
Memw•7mo ago
ah right it's interpolated, interpolated strings can't do that do Encoding.UTF8.GetBytes("..")
DarkAngel
DarkAngelOP•7mo ago
No description
DarkAngel
DarkAngelOP•7mo ago
like that?
Memw
Memw•7mo ago
yeah
DarkAngel
DarkAngelOP•7mo ago
i'll test that it still removes the preceding 2nd text
Memw
Memw•7mo 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
DarkAngel
DarkAngelOP•7mo ago
file content before : History of angethemessager : - 754 : name ★ (ressource) file content after : History of angethemessager : - 755 : name ★ (ressource)
Memw
Memw•7mo 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
DarkAngel
DarkAngelOP•7mo ago
No description
Memw
Memw•7mo ago
ahh
DarkAngel
DarkAngelOP•7mo ago
this is the only second time i use the file management there
Memw
Memw•7mo ago
you are creating the file and reopening it do this wait
DarkAngel
DarkAngelOP•7mo ago
(it's in another .cs file)
Memw
Memw•7mo ago
AHH first of all
Memw
Memw•7mo ago
name missmatch
No description
Memw
Memw•7mo ago
Historigue and Historique
DarkAngel
DarkAngelOP•7mo ago
oh 🤦
Memw
Memw•7mo ago
so it recreates the file because Historigue never exists and also you could improve efficiency by doing this
DarkAngel
DarkAngelOP•7mo ago
well yeah it's that one error that removed all the text each time 🤦
Memw
Memw•7mo 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
DarkAngel
DarkAngelOP•7mo ago
alrighty thanks ^^
Want results from more Discord servers?
Add your server