CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: StreamWriter?

  1. #1
    Join Date
    Jun 2010
    Posts
    5

    StreamWriter?

    Hello.
    I was wondering if someone could tell me why my script isn't working?

    I expect it to write "Hello word" to log.txt on my desktop?

    Code:
        public struct _cnf
        {
            public string logS;
        }
    
         static void form1(_cnf cnf)
         {
             cnf.logS = "C:\\Users\\Tom\\Desktop\\log.txt";
             StreamWriter logF = new StreamWriter(cnf.logS);
             logF.WriteLine("Hello World");
         }
    There are no compiling errors, it just doesn't write anything in log.txt

    any ideas?

    thanks.

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: StreamWriter?

    Close the streamwriter or call Flush on it. Your application is exiting before the internal buffer in StreamWriter has been flushed to disk.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: StreamWriter?

    You are never flushing you stream. You write to the stream, which is in memory, but you never flush it to disk and you also do not dispose of it. Use this:

    Code:
    using ( StreamWriter writer = new StreamWriter( @"C:\foo.txt" ) )
    {
        writer.Write( "Hello" );
        writer.Flush( );
    }
    EDIT: Ninja'd

  4. #4
    Join Date
    Jun 2010
    Posts
    5

    Re: StreamWriter?

    Hello

    Thank you for your help! works now

    It prints my message to the file, but it puts the cursor to the very beggining, so If I print more, it tends to overwrite it I think? How do I make it go at the bottom so it prints the next lot below it?

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured