[RESOLVED] Streamwriter does NOT write complete file
:confused: 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.:confused:
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...
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?
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.
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.:confused:
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?
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).
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()
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.;) :wave:
Re: [RESOLVED] Streamwriter does NOT write complete file
I was having the same problem and this solved it. Thank you!!!!