FileMode.OpenOrCreate - bug?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marty McDonald

    #1

    FileMode.OpenOrCreate - bug?

    Sometimes I use FileMode.OpenOr Create to write data to a file. Then, when I
    try to write to that file again, remanants of the first set of data exist in
    the file.
    I'd understand if it was trying to append or something, but when only part
    of the first set of data exists, I think it's an error. If anyone has any
    information on this, it would be good... For now I will just warn the users
    of this class, so no resolution is necessary...
    Thanks!

    Code follows for those interested...
    using System;

    using System.Data;

    using System.IO;

    namespace WSDOT.Spectrum

    {

    /// <summary>

    /// Will write data to a file, delimiting each field by a specified
    delimiter.

    /// </summary>

    /// <remarks>Certai n characters may adversely affect results. For example,
    if

    /// the chosen delimiter is a semi-colon, existing semi-colons in the data
    itself may cause problems for

    /// those applications that consume the resultant file.

    /// <p>Newline characters in the data itself may cause a line break where
    one might not be wanted.

    /// </p>

    /// <p>If the data contains columns of special data types (such as 'image'),
    the string 'System.Byte[]'

    /// will be written in place of the actual value.

    /// </p>

    /// <p>Use FileMode.OpenOr Create with caution - its behavior is
    unpredictable

    /// when used against a file that already contains data.

    /// </p>

    /// <p>It is the programmer's responsibility to anticipate and respond to
    such situations.

    /// </p>

    /// </remarks>

    public class DelimitedWriter

    {

    /// <summary>

    /// Constructor is private so class cannot be instantiated.

    /// </summary>

    private DelimitedWriter ()

    {

    }

    /// <summary>

    /// Will persist given DataTable to a delimited file.

    /// </summary>

    /// <param name="dataTable ">DataTable containing data to write.</param>

    /// <param name="fileName" >File to write to.</param>

    /// <param name="mode">Spe cifies how the operating system should write to
    the file.

    /// Use FileMode.OpenOr Create with caution - its behavior is unpredictable
    when used against a file that already contains data.</param>

    /// <param name="delimiter ">Delimiter to use.</param>

    /// <param name="includeCo lumnHeadings">I f true, will include column
    headings.</param>

    /// <returns>True if successful.</returns>

    public static bool CreateDelimited File(DataTable dataTable,strin g fileName,
    FileMode mode, Char delimiter, bool includeColumnHe adings)

    {

    FileStream fs = new FileStream(file Name,mode,FileA ccess.Write);

    StreamWriter sw = new StreamWriter(fs );


    //The "seek" method fixes an issue which is...

    //If a file is created with

    string line = "";

    //Write the column headings if desired.

    if (includeColumnH eadings)

    {

    for (Int32 i = 0; i < dataTable.Colum ns.Count; i++)

    {

    line += dataTable.Colum ns[i].ColumnName + delimiter;

    }

    sw.WriteLine(li ne);

    line = "";

    }

    //Now write the actual values.

    for (Int32 i = 0; i < dataTable.Rows. Count; i++) //loop through each row

    {

    for (Int32 z = 0; z < dataTable.Colum ns.Count; z++) //loop throuch each
    column

    {

    line += dataTable.Rows[i][z].ToString() + delimiter;

    }

    sw.WriteLine(li ne);

    line = "";

    }

    sw.Close();

    fs.Close();

    return true;

    }

    /// <summary>

    /// Will persist first table in a DataSet to a delimited file.

    /// </summary>

    /// <param name="dataSet"> DataSet containing data to persist.</param>

    /// <param name="fileName" >File to write to.</param>

    /// <param name="mode">Spe cifies how the operating system should write to
    the file.

    /// Use FileMode.OpenOr Create with caution - its behavior is unpredictable
    when used

    /// against a file that already contains data.

    /// </param>

    /// <param name="delimiter ">Delimiter to use.</param>

    /// <param name="includeCo lumnHeadings">I f true, will include column
    headings.</param>

    /// <returns>True if successful.</returns>

    public static bool CreateDelimited File(DataSet dataSet, string fileName,
    FileMode mode, Char delimiter, bool includeColumnHe adings)

    {

    return
    CreateDelimited File(dataSet.Ta bles[0],fileName,mode, delimiter,inclu deColumnH
    eadings);

    }

    /// <summary>

    /// Will persist ordinally-numbered table in a DataSet to a delimited file.

    /// </summary>

    /// <param name="dataSet"> DataSet containing data to persist.</param>

    /// <param name="whichTabl e">Ordinal table number to persist.</param>

    /// <param name="fileName" >File to write to.</param>

    /// <param name="mode">Spe cifies how the operating system should write to
    the file.

    /// Use FileMode.OpenOr Create with caution - it's behavior is unpredictable
    when used

    /// against a file that already contains data.

    /// </param>

    /// <param name="delimiter ">Delimiter to use.</param>

    /// <param name="includeCo lumnHeadings">I f true, will include column
    headings.</param>

    /// <returns>True if successful.</returns>

    public static bool CreateDelimited File(DataSet dataSet,Int32 whichTable,
    string fileName, FileMode mode, Char delimiter, bool includeColumnHe adings)

    {

    return
    CreateDelimited File(dataSet.Ta bles[whichTable],fileName,mode, delimiter,inclu
    deColumnHeading s);

    }

    /// <summary>

    /// Will persist named table in a DataSet to a delimited file.

    /// </summary>

    /// <param name="dataSet"> DataSet containing data to persist.</param>

    /// <param name="whichTabl e">Named table to persist.</param>

    /// <param name="fileName" >File to write to.</param>

    /// <param name="mode">Spe cifies how the operating system should write to
    the file.

    /// Use FileMode.OpenOr Create with caution - it's behavior is unpredictable
    when used

    /// against a file that already contains data.

    /// </param>

    /// <param name="delimiter ">Delimiter to use.</param>

    /// <param name="includeCo lumnHeadings">I f true, will include column
    headings.</param>

    /// <returns>True if successful.</returns>

    public static bool CreateDelimited File(DataSet dataSet,string whichTable,
    string fileName, FileMode mode, Char delimiter, bool includeColumnHe adings)

    {

    return
    CreateDelimited File(dataSet.Ta bles[whichTable],fileName,mode, delimiter,inclu
    deColumnHeading s);

    }



    }//end class

    }



  • Tian Min Huang

    #2
    RE: FileMode.OpenOr Create - bug?

    Hello Marty,

    I did some research, however, I did not find any known issue about this
    problem. I'd like to share the following information with you:

    1. When a file already exists, FileMode.OpenOr Create is the same with
    FileMode.Open. The consequent write operation will overwrite the content of
    the file from the beginning.

    2. If you want to keep the existing file data, I recommend you use the
    following file mode:
    FileMode.OpenOr Create | FileMode.Trunca te

    Otherwise, you should use the file mode below:
    FileMode.OpenOr Create | FileMode.Append

    3. If the problem persists on your side, could you please tell me the
    detailed steps to reproduce the problem? I will be glad to check it on my
    side.

    I look forward to hearing from you.

    Have a nice day!

    Regards,

    HuangTM
    Microsoft Online Partner Support
    MCSE/MCSD

    Get Secure! ¨C www.microsoft.com/security
    This posting is provided ¡°as is¡± with no warranties and confers no rights.


    Comment

    • Tian Min Huang

      #3
      RE: FileMode.OpenOr Create - bug?

      Hi Marty,

      Please feel free to let me know if you have any problems or concerns.

      Have a nice day!

      Regards,

      HuangTM
      Microsoft Online Partner Support
      MCSE/MCSD

      Get Secure! ¨C www.microsoft.com/security
      This posting is provided ¡°as is¡± with no warranties and confers no rights.


      Comment

      Working...