Results 1 to 10 of 10

Thread: [RESOLVED] Streamwriter does NOT write complete file

  1. #1

    Thread Starter
    Addicted Member SidCMC's Avatar
    Join Date
    Jan 2008
    Location
    Mumbai, India
    Posts
    166

    Resolved [RESOLVED] Streamwriter does NOT write complete file

    Hello Friends,
    I have a code which reads a text file through streamreader,
    selects what is important and writes the same to another file
    using streamwriter. The problem is that the streamwriter stops
    writing beyond a certain line
    and just doesnt write ahead.
    What can be the problem? Do I have to make two seperate files?
    Regards,
    Sid.
    THE BEST WAY TO THANK A HELPFUL POST IS TO RATE IT!!!!
    Take up one idea. Make that one idea your life - think of it, dream of it, live on that idea. Let the brain, muscles, nerves, every part of your body, be full of that idea, and just leave every other idea alone.

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Streamwriter does NOT write complete file

    Rather than forcing us to guess what is wrong with the code, how about showing it, that might be a good start...
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Addicted Member SidCMC's Avatar
    Join Date
    Jan 2008
    Location
    Mumbai, India
    Posts
    166

    Question Re: Streamwriter does NOT write complete file

    Ok, its something like this............

    Code:
    dim tempstr as string
    dim d as new directoryinfo("c:\temp", "*.txt")
    dim files() as string = d.getfiles()
    dim count as integer = files.getlength(0)             'thats the first dimension
    dim start as integer
    dim str as new streamwriter("C:\logfile.txt")
    
    for start = 0 to count 
    dim f as new filestream(files(start), createmode, writemode)
    dim s as new streamreader(f)
    
    do 
    
    tempstr = s.readline()
    str.writeline(tempstr)
    
    
    While Not s Is Nothing
    *****************************************************
    Now when I check out the "logfile.txt"written it is incomplete. Only about
    75% of the file is written. Also there are too many text files in the C:\temp
    folder(about 3 GB)...What might be the reason?
    THE BEST WAY TO THANK A HELPFUL POST IS TO RATE IT!!!!
    Take up one idea. Make that one idea your life - think of it, dream of it, live on that idea. Let the brain, muscles, nerves, every part of your body, be full of that idea, and just leave every other idea alone.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Streamwriter does NOT write complete file

    you're looping until s is nothing. S will not become 'Nothing' just because it reaches the end of the line. Loop until s.EndOfStream instead.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Addicted Member SidCMC's Avatar
    Join Date
    Jan 2008
    Location
    Mumbai, India
    Posts
    166

    Question Re: Streamwriter does NOT write complete file

    Hi,
    I guess s.Endofstream method is not there
    in Framework 1.0 ...and anyways why is 75%
    of the file getting written and the remaining
    does not?
    Regards,
    Sid.
    THE BEST WAY TO THANK A HELPFUL POST IS TO RATE IT!!!!
    Take up one idea. Make that one idea your life - think of it, dream of it, live on that idea. Let the brain, muscles, nerves, every part of your body, be full of that idea, and just leave every other idea alone.

  6. #6

    Thread Starter
    Addicted Member SidCMC's Avatar
    Join Date
    Jan 2008
    Location
    Mumbai, India
    Posts
    166

    Re: Streamwriter does NOT write complete file

    Lets say that out of 100 lines , this code reads 75 lines.
    The only change I made is ::

    Code:
    While Not temstr Is Nothing
    Has it got something to do with the space restriction in the logfile.txt?
    THE BEST WAY TO THANK A HELPFUL POST IS TO RATE IT!!!!
    Take up one idea. Make that one idea your life - think of it, dream of it, live on that idea. Let the brain, muscles, nerves, every part of your body, be full of that idea, and just leave every other idea alone.

  7. #7

    Thread Starter
    Addicted Member SidCMC's Avatar
    Join Date
    Jan 2008
    Location
    Mumbai, India
    Posts
    166

    Re: Streamwriter does NOT write complete file

    When I debug the program ... the readline() function reads the entire file but when I check the logfile.txt, the file is incomplete(i.e complete file
    has not been copied).
    THE BEST WAY TO THANK A HELPFUL POST IS TO RATE IT!!!!
    Take up one idea. Make that one idea your life - think of it, dream of it, live on that idea. Let the brain, muscles, nerves, every part of your body, be full of that idea, and just leave every other idea alone.

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Streamwriter does NOT write complete file

    Try something like this:
    Code:
    Dim file1path As String = "path to file1 here"
    Dim file2path As String = "path to file2 here"
    Dim reader As New IO.StreamReader(file1path)
    Dim writer As New StreamWriter(file2path)
    While reader.Peek <> -1
          writer.WriteLine(reader.ReadLine())
    End While
    reader.Close()
    writer.Close()

  9. #9

    Thread Starter
    Addicted Member SidCMC's Avatar
    Join Date
    Jan 2008
    Location
    Mumbai, India
    Posts
    166

    Resolved Re: Streamwriter does NOT write complete file

    Hi Stanav!!!!
    I wasnt closing the streamreader and streamwriter objects
    I tried flushing the streamwriter object and it is working
    fabulously. Thank you,
    Sid.
    THE BEST WAY TO THANK A HELPFUL POST IS TO RATE IT!!!!
    Take up one idea. Make that one idea your life - think of it, dream of it, live on that idea. Let the brain, muscles, nerves, every part of your body, be full of that idea, and just leave every other idea alone.

  10. #10
    New Member
    Join Date
    Jun 2012
    Posts
    1

    Smile Re: [RESOLVED] Streamwriter does NOT write complete file

    I was having the same problem and this solved it. Thank you!!!!

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