File writes in a loop



I need to write data to a file in a loop. I get a "file already in
use" error on the second loop. The problem is, I don't know the name
of the file until I'm in the loop. How can I keep a file open for
writing in a loop?

while ((data = log.ReadLine()) != null)
{
string date = data.Substring(0, 10);
FileStream fileOut = new FileStream(dir + date + name,
FileMode.OpenOrCreate, FileAccess.Write);

fileOut.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
fileOut.Flush();
}
.



Relevant Pages

  • Re: using a for loop to determine maximum value of an int variable
    ... I cant understand what is going on in the second loop (or more ... It does this by first trying to go half the distance. ... The first loop finds the highest bit stored in the number, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Changing connection information on OLEDB connection
    ... What I would do is set a breakpoint on the "On Each Iteration Of The Loop" event. ... I would then set a watch on the variable to make sure that the connection string is being set properly. ... When I run the package, I just get the first server's data twice, it ... but the second loop around it doesn't seem to be working. ...
    (microsoft.public.sqlserver.dts)
  • Re: generic strncpy - off-by-one error
    ... My first loop exits either with the number of bytes remaining in the ... My second loop has an implicit comparison against zero. ... comparison because the decrement is in the loop conditional. ...
    (Linux-Kernel)
  • RE: generic strncpy - off-by-one error
    ... of the second loop needs an explicit comparison with -1, ... second loop). ... > upon exiting the first loop. ... > This is only a win for relatively long nul padding. ...
    (Linux-Kernel)
  • RE: foreach behavior changed in Framework 2.0
    ... creating two foreach loops, the first just outputs an ordinal value that gets ... incremented each time through the loop and a unique id of the row; ... I put a try/catch block around the output ... statement in the second loop and in the catch block I output the ordinal ...
    (microsoft.public.dotnet.framework.adonet)

Loading