# textwriter.close deletes my data

This is a discussion on textwriter.close deletes my data within the C# Programming forums, part of the General Programming Boards category; hey, i got this weird problem. i am making a little textediting program and somehow when i have written a ...

1. ## textwriter.close deletes my data

hey,

i got this weird problem.
i am making a little textediting program and somehow when i have written a txt file and close it via:

tc.Close();

the text file goes blank. nothing left.

the code ( sorry if it looks like rubbish. i just started programming in C#) :

Code:
 static void opentxtfile(string input)
{
Console.Clear();
if (input == " ")
{
Console.WriteLine("TEXTEDITOR \n \nGive the name of the text file, or 1 to exit\n\n");
Console.Write("file:   ");
if (input2 == "1" )
{
txtedit();
return;
}
else
opentxtfile(input2);
return;
}
else
if (!File.Exists(MAINDIR + "\\documents\\text\\" + input + ".txt"))
{
Console.WriteLine("text file does not exist.");
txtedit();
return;
}
Int32 NumberOfLines = 1;

using (StreamReader r = new StreamReader(MAINDIR + "\\documents\\text\\" + input + ".txt"))
{
int i = 0;
while (r.ReadLine() != null) { i++; }
NumberOfLines = i + 1;

}

string[] ListLines = new string[NumberOfLines+1];
for (int i = 1; i < NumberOfLines; i++)
{
}

Console.Clear();
for (int y = 1; y < NumberOfLines; y++)
{
Console.WriteLine(ListLines[y]);
}
tw.Close();

TextWriter tc = new StreamWriter (MAINDIR + "\\documents\\text\\" + input + ".txt");

if (newline != "/quit")
{

int a;
for (a = 1; a < NumberOfLines; a++)
{
tc.WriteLine(ListLines[a]);
}
ListLines[NumberOfLines] = newline;
tc.WriteLine(ListLines[NumberOfLines]);
tc.Close();
opentxtfile(input);
return;
}
else
tc.Close(); //  right here
txtedit();
return;

}

2. Hi there. It's difficult to fully understand the purpose of your program because the txtedit() function is missing, but I am guessing you want to:

• get a valid filename
• output the contents to the console
• allow the user to enter additional lines of text which will be appended to the end of the file.
• stop adding new lines when they type /quit

Since you've clearly made an effort to do this for yourself, I don't see the harm in offering you another perspective on solving this task. So assuming I've correctly identified the requirements for your program, this is what I'd do:

Code:
static void Main(string[] args)
{
String filename = String.Empty;

while (true) // find a file
{
Console.Write("enter filename: ");

if (File.Exists(filename))
break; // filename is valid, let's move on
}

using (StreamWriter writer = File.AppendText(filename)) // write to the file
{
while (true)
{

if (str == "/quit")
break; // we are done adding more

writer.WriteLine(str); // add next line to the file
}
}
}
I'm not entirely sure why your version didn't work. Maybe it's because you have a TextReader and a StreamReader open to the file at the same time. (By the way, you don't appear to use the TextReader at all.) Maybe TextWriters don't allow appending. I've never used them before. Perhaps that is something you could investigate.

Hope this helps.

3. Originally Posted by theoobe
Maybe it's because you have a TextReader and a StreamReader open to the file at the same time.
According to the msdn (TextWriter Class (System.IO)) TextWriter is not thread safe, and I see in his example that he's got Thread() stuff happening. I don't know enough C# to follow everything happening here, but it's a point to check on maybe.

Quzah.