Streamwriter not writing

This is a discussion on Streamwriter not writing within the C# Programming forums, part of the General Programming Boards category; HI i am trying to save my information to a file, i have looked at various examples which say the ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    85

    Streamwriter not writing

    HI

    i am trying to save my information to a file, i have looked at various examples which say the following should work

    Code:
    Stream myStream;
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    
                saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFileDialog1.FilterIndex = 1;
                saveFileDialog1.RestoreDirectory = true;
    
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if ((myStream = saveFileDialog1.OpenFile()) != null)
                    {
                        try
                        {
                           StreamWriter wText = new StreamWriter(myStream);
    
                            wText.Write("Test Text");
                            
                                                        
                            // Code to write the stream goes here.
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                        }
    
                            myStream.Close();
                    }
                }
    i pass the filename i need throught the savedialog box, and have tried changing

    StreamWriter wText = new StreamWriter(myStream);
    to
    StreamWriter wText = new StreamWriter(saveFileDialog1.Filename); but it throughs an exception as the stream is already open

    i have tried changing the wText.Write to wText.WriteLine but that didnt help.

    this creates the file ok but the file is empty - i.e. my "Test Text" is not written to the created file.

    am i missing something here or should this work?

    i eventually need to write out a List<> to this file and be able to read it back in again.
    im guessing i am going to have to delimit the file in some way for when i read it back in as my List<> contents will be varying length ?

    for the moment if i can just get it to put that text in the file i can move forward on testing the rest.

    many thanks
    Last edited by deviousdexter; 11-24-2008 at 09:18 AM.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Yes you will need a delimiter.

    Code:
                SaveFileDialog sd = new SaveFileDialog();
                sd.ShowDialog();
    
    
                StreamWriter sw = new StreamWriter(sd.OpenFile());
    
                sw.Write("Hello");
    
                sw.Close();

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    OK sorry have got it sorted.

    its needed a

    wText.Flush();

    after the wText.Write("Test Text");

    cant believe how many articles i looked at that did not have this line

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by deviousdexter View Post
    OK sorry have got it sorted.

    its needed a

    wText.Flush();

    after the wText.Write("Test Text");

    cant believe how many articles i looked at that did not have this line
    Surely if you close the stream, that should flush it too...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    well it appears to be writing properly now with the flush and wasnt without it so i dont know

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by deviousdexter View Post
    well it appears to be writing properly now with the flush and wasnt without it so i dont know
    But at least the code you have posted above, doesn't actually close the wText stream.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    ahh thanks Mat - i see your point - iam closing the "myStream" but not the "wText" stream. ill try it that way without the flush and see how i go.

    thanks for drawing my attention to that fact.

    EDIT: tried and working
    Last edited by deviousdexter; 11-24-2008 at 09:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing encrypted value to file.
    By mkthnx001 in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 01:42 PM
  2. Replies: 2
    Last Post: 05-20-2008, 09:57 AM
  3. Very slow file writing of 'fwrite' function in C
    By scho in forum C Programming
    Replies: 6
    Last Post: 08-03-2006, 03:16 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 10:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21