Thread: textwriter.close deletes my data

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    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:   ");
                    string input2 = Console.ReadLine();
                    Thread.Sleep(1);
                    if (input2 == "1" )
                    {
                        txtedit();
                        return;
                    }
                    else
                    opentxtfile(input2);
                    return;
                }
                else
                Thread.Sleep(1);
                if (!File.Exists(MAINDIR + "\\documents\\text\\" + input + ".txt"))
                {
                    Console.WriteLine("text file does not exist.");
                    txtedit();
                    return;
                }
                TextReader tw = new StreamReader(MAINDIR + "\\documents\\text\\" + input + ".txt");
                Int32 NumberOfLines = 1;
                
    
    
               
                Thread.Sleep(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++)
                {
                    ListLines[i] = tw.ReadLine();
                }
                
                Console.Clear();
                for (int y = 1; y < NumberOfLines; y++)
                {
                    Console.WriteLine(ListLines[y]);
                }
                tw.Close();
                Thread.Sleep(1);
                string newline = Console.ReadLine();
    
                TextWriter tc = new StreamWriter (MAINDIR + "\\documents\\text\\" + input + ".txt");
    
                if (newline != "/quit")
                {
    
                    Thread.Sleep(5);
                    int a;
                    for (a = 1; a < NumberOfLines; a++)
                    {
                        tc.WriteLine(ListLines[a]);
                    }
                    Thread.Sleep(5);
                    ListLines[NumberOfLines] = newline;
                    tc.WriteLine(ListLines[NumberOfLines]);
                    tc.Close();
                    Thread.Sleep(5);
                    opentxtfile(input);
                    return;
                }
                else
                tc.Close(); //  right here 
                Thread.Sleep(500);
                txtedit();
                return;
                
            }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    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
    • read that file
    • 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: ");
            filename = Console.ReadLine();
    
            if (File.Exists(filename))
                break; // filename is valid, let's move on
            else Console.WriteLine("file not found");
        }
    
        using (StreamReader reader = File.OpenText(filename)) // open the file
            while (!reader.EndOfStream) // read to the end of the file
                Console.WriteLine(reader.ReadLine()); // print the next line from the file
    
        using (StreamWriter writer = File.AppendText(filename)) // write to the file
        {
            while (true)
            {
                String str = Console.ReadLine();
    
                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.
    Last edited by theoobe; 10-08-2011 at 04:43 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by theoobe View Post
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot write to a closed TextWriter
    By zavzen in forum C# Programming
    Replies: 2
    Last Post: 11-20-2008, 03:40 AM
  2. How write class like vector that deletes self?
    By 6tr6tr in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2008, 11:46 AM
  3. Replies: 2
    Last Post: 12-05-2007, 10:56 AM
  4. Visual Studio designer deletes my user control
    By bennyandthejets in forum C# Programming
    Replies: 1
    Last Post: 05-06-2005, 07:44 AM
  5. deletes
    By hen in forum C Programming
    Replies: 7
    Last Post: 08-17-2002, 08:56 AM