473,509 Members | 6,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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();
}
Jun 27 '08 #1
10 2452
while ((data = log.ReadLine()) != null)
{
string date = data.Substring(0, 10);
using (FileStream fileOut = new FileStream(dir + date + name,
FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
fileOut.Write(bytes, 0, bytes.Length);
}
<Ad*********@gmail.comwrote in message
news:bc**********************************@l64g2000 hse.googlegroups.com...
>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();
}

Jun 27 '08 #2
On May 16, 3:35 pm, AdemusPr...@gmail.com wrote:
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();

}
are you going have multiple files open in the loop?if yes and you
dont want to close them then you could
use a a hashtable and use dir + date + name as the key and the
filestream as the object.
Jun 27 '08 #3
On May 16, 3:35*pm, AdemusPr...@gmail.com wrote:
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();

}
Your question is cryptic, so I am just guessing here but maybe you
need a:

fileOut.Close();

after the flush?

And maybe even a:

fileOut.Dispose();
Jun 27 '08 #4
Thank you for the reply.

This code will close, dispose and reopen the file on each loop.
Therefore, after each loop there is only one record in the output file
which is a last write.
Jun 27 '08 #5
Not sure which part is cryptic. I need to open the file for writing
and keep it open in a loop.

I can close and/or dispose the object but I want to keep the file open
for writing instead of making roundtrip opens/closes each time. Plus,
i would have to advance the cursor to the end of the file each time I
open it, which could be big.
Jun 27 '08 #6
<Ad*********@gmail.comwrote:
I need to write data to a file in a loop. I get a "file already in
use" error on the second loop.
You aren't closing the file. After calling Flush, call Close.

Eq.
Jun 27 '08 #7
On Fri, 16 May 2008 13:08:07 -0700, <Ad*********@gmail.comwrote:
Not sure which part is cryptic. I need to open the file for writing
and keep it open in a loop.
You keep saying "the file", but then you also imply that you may have more
than one file.
I can close and/or dispose the object but I want to keep the file open
for writing instead of making roundtrip opens/closes each time.
If you have more than one file you might open in the loop, then you need
to keep track of all the files you've opened. You've already been
provided suggestions for doing that.

If you only have one file, then I recommend putting the FileStream
variable outside the loop, initialized to null, and creating the
FileStream instance only if the variable is null. If it's not null, just
use the current value of the variable.
Plus,
i would have to advance the cursor to the end of the file each time I
open it, which could be big.
The size of the file make absolutely no difference with respect to
seeking. You could in fact close and reopen the file each iteration of
the loop, seeking to the end of the file before writing each time (or just
use FileMode.Append instead of OpenOrCreate). The only real performance
overhead would be the act of opening and closing the file; the seeking
isn't a problem at all.

But the fact is, there's not any need to reopen the file each time through
the loop. Whether you have more than one file or not, there is a
perfectly good solution that doesn't involve reopening the file with each
iteration of the loop.

Pete
Jun 27 '08 #8
On Fri, 16 May 2008 13:08:07 -0700 (PDT), Ad*********@gmail.com wrote:
>Not sure which part is cryptic. I need to open the file for writing
and keep it open in a loop.

I can close and/or dispose the object but I want to keep the file open
for writing instead of making roundtrip opens/closes each time. Plus,
i would have to advance the cursor to the end of the file each time I
open it, which could be big.
You need to close the file after flushing it. If you need to add to
the file in a subsequent iteration, I would first check to see if the
file exists yet or not with the File.Exists method.

If if does not exist, open the file for create or open. If it does,
open it for append.
Jun 27 '08 #9
Joe,

In my idea do you give the best solution

(This in fact to the OP to try this first).

Cor

"Joe Cool" <jo*****@home.netschreef in bericht
news:ph********************************@4ax.com...
On Fri, 16 May 2008 13:08:07 -0700 (PDT), Ad*********@gmail.com wrote:
>>Not sure which part is cryptic. I need to open the file for writing
and keep it open in a loop.

I can close and/or dispose the object but I want to keep the file open
for writing instead of making roundtrip opens/closes each time. Plus,
i would have to advance the cursor to the end of the file each time I
open it, which could be big.

You need to close the file after flushing it. If you need to add to
the file in a subsequent iteration, I would first check to see if the
file exists yet or not with the File.Exists method.

If if does not exist, open the file for create or open. If it does,
open it for append.
Jun 27 '08 #10
This is what I ended up with.

using (StreamReader log = File.OpenText(myArgs["i"]))
{
FileStream outFile = null;
string old = string.Empty;
while ((data = log.ReadLine()) != null)
{
string date = data.Substring(0, 10);
if (old != date)
{
if (outFile != null)
{
outFile.Close();
}
outFile = new FileStream(dir + date +
name, FileMode.Append, FileAccess.Write);
old = date;
}
outFile.Write(Encoding.UTF8.GetBytes(data), 0,
data.Length);
outFile.Write(Encoding.UTF8.GetBytes("\r\n"),
0, 2);
outFile.Flush();
}
outFile.Close();
}
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2666
by: Chuck | last post by:
Problem: to rename a file sent to a PDF writer A macro opens a report in print mode the printer is a pdf writer The report name is: "Directory.pdf" The desired name is: "Directory " &...
4
1498
by: phantom | last post by:
Hi All. I am having a problem writing to a file. I can successfully open the file and write the contents using fprintf, however if the writing is not done for a while in the process the file...
7
2232
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
26
2972
by: Michel Rouzic | last post by:
I have a binary file used to store the values of variables in order to use them again. I easily know whether the file exists or not, but the problem is, in case the program has been earlier...
5
1575
by: Protoman | last post by:
Here's a program I wrote that calcs the fibonacci numbers and writes them to a file, from 1-50. It prints to the screen just fine, but it only prints the last number, Fib(50) to the file. Code:...
3
7088
by: Avi | last post by:
I need to create a text file that has the data from the 10 tables in the database. The number of fields in the tables exceeds 255 and so I cannot make a new table with all the fields and then...
2
1922
by: Suman | last post by:
Happy Friday everyone!!! I am working on a windows service and a C# application and needed some help with certain functionality. Please read through my issue below. Thanks! I have a windows...
2
4184
by: Bonzol | last post by:
vb.net 2003 Windows application We have a Client/Server system set up by communicating through a TCPClient object. The server loops continuously using a tcplistener device and each time a client...
0
1698
grassh0pp3r
by: grassh0pp3r | last post by:
Hello, I'm trying to make a very simple comments page on my site using PHP and am having problems somewhere. I am very new to PHP. I was able to create one that works with comments appended, but...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7416
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5656
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.