File writes in a loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AdemusPrime@gmail.com

    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.OpenOr Create, FileAccess.Writ e);

    fileOut.Write(E ncoding.UTF8.Ge tBytes(data), 0, data.Length);
    fileOut.Flush() ;
    }
  • Hilton

    #2
    Re: File writes in a loop

    while ((data = log.ReadLine()) != null)
    {
    string date = data.Substring( 0, 10);
    using (FileStream fileOut = new FileStream(dir + date + name,
    FileMode.OpenOr Create, FileAccess.Writ e))
    {
    byte[] bytes = Encoding.UTF8.G etBytes(data);
    fileOut.Write(b ytes, 0, bytes.Length);
    }


    <AdemusPrime@gm ail.comwrote in message
    news:bcddf171-9e0b-4fbe-9069-171aedc308e4@l6 4g2000hse.googl egroups.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.OpenOr Create, FileAccess.Writ e);
    >
    fileOut.Write(E ncoding.UTF8.Ge tBytes(data), 0, data.Length);
    fileOut.Flush() ;
    }

    Comment

    • parez

      #3
      Re: File writes in a loop

      On May 16, 3:35 pm, AdemusPr...@gma il.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.OpenOr Create, FileAccess.Writ e);
      >
      fileOut.Write(E ncoding.UTF8.Ge tBytes(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.

      Comment

      • zacks@construction-imaging.com

        #4
        Re: File writes in a loop

        On May 16, 3:35 pm, AdemusPr...@gma il.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.OpenOr Create, FileAccess.Writ e);
        >
        fileOut.Write(E ncoding.UTF8.Ge tBytes(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 ();

        Comment

        • AdemusPrime@gmail.com

          #5
          Re: File writes in a loop

          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.

          Comment

          • AdemusPrime@gmail.com

            #6
            Re: File writes in a loop

            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.

            Comment

            • Paul E Collins

              #7
              Re: File writes in a loop

              <AdemusPrime@gm ail.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.


              Comment

              • Peter Duniho

                #8
                Re: File writes in a loop

                On Fri, 16 May 2008 13:08:07 -0700, <AdemusPrime@gm ail.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

                Comment

                • Joe Cool

                  #9
                  Re: File writes in a loop

                  On Fri, 16 May 2008 13:08:07 -0700 (PDT), AdemusPrime@gma il.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.

                  Comment

                  • Cor Ligthert[MVP]

                    #10
                    Re: File writes in a loop

                    Joe,

                    In my idea do you give the best solution

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

                    Cor

                    "Joe Cool" <joecool@home.n etschreef in bericht
                    news:phtr24lgfh 3o6k5rttivbv9em ladtr16tp@4ax.c om...
                    On Fri, 16 May 2008 13:08:07 -0700 (PDT), AdemusPrime@gma il.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.

                    Comment

                    • AdemusPrime@gmail.com

                      #11
                      Re: File writes in a loop

                      This is what I ended up with.

                      using (StreamReader log = File.OpenText(m yArgs["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.Writ e);
                      old = date;
                      }
                      outFile.Write(E ncoding.UTF8.Ge tBytes(data), 0,
                      data.Length);
                      outFile.Write(E ncoding.UTF8.Ge tBytes("\r\n"),
                      0, 2);
                      outFile.Flush() ;
                      }
                      outFile.Close() ;
                      }

                      Comment

                      Working...