麻烦让我的方法循环

时间:2016-06-17 18:38:36

标签: c# csv methods logic

我制作了一个小型CSV转换程序,我的一个名为OrganizeDataAndCreateOutput()的方法正在调用,但没有循环打印出我的记录文件中的每个字段。 此刻我的while循环是空的,因为我不知道如何构建它。

代码中的其他所有内容都非常好,但似乎我不知道如何让这个方法循环。我正在使用全局静态变量,但在这种情况下似乎无法用来帮助我。

该方法应该打印出每个字段,但是跳过字段中的逗号,这样字段就不会被分开"。

谁能看到我错过的东西?我是新人,但我希望尽可能多地学习!

这是我的代码

class Program
{
    // File object variables
    static TextFieldParser input = new TextFieldParser("PPVendingPricing.csv");
    static StreamWriter output = new StreamWriter("convertedInventoryItemListTacMed.csv");

    // Input and output buffer variables
    static string[] inputBuffer;
    static string[] outputBuffer = new string[40];


    static void Main(string[] args)
    { 
        ReadInputAndBuildDataStructures();
        OrganizeDataAndCreateOutput();

        Console.WriteLine("done");
        input.Close();
        output.Close();
        Console.Read();
    }

     /*
     * This method reads the input file and fill data structures
     * that are used to organize the data before moving selected
     * fields to the output buffer.
     */ 
    public static void ReadInputAndBuildDataStructures()
    {
        input.SetDelimiters(",");

        input.ReadFields(); // Skip the header record.

        while (!input.EndOfData)
        {
            inputBuffer = input.ReadFields(); // Read a CSV record in to the inputBuffer. 
        }
    }

    /*
    * This method loads default values into the output
    * buffer (string array). Some of these values will be 
    * replaced before the output buffer is written to the file.
    */
    public static void SetOutputBufferDefaultValues()
    {
        // Initialize all fields to empty.
        for (int i = 0; i < outputBuffer.Length; i++)
        {
            outputBuffer[i] = "";
        }

        // Update selected fields with default values.
        outputBuffer[7] = "Solutions Inc";
        outputBuffer[10] = "TRUE";
        outputBuffer[11] = "FIFO";
        outputBuffer[15] = "TRUE";
        outputBuffer[17] = "Main";
        outputBuffer[19] = "TRUE";
        outputBuffer[21] = "Solutions Inc";
        outputBuffer[25] = "Main";
        outputBuffer[28] = "Periods of Supply";
        outputBuffer[32] = "1";
        outputBuffer[35] = "By Overall Item Qty";
        outputBuffer[36] = "TRUE";
        outputBuffer[37] = "TRUE";
    }

    /*
    * This method maps selected values from the input buffer
    * to the appropriate position in the output buffer.
    */
    public static void MapInputFieldsToOutputFields()
    {
        outputBuffer[0] = inputBuffer[26];
        outputBuffer[1] = inputBuffer[38];
        outputBuffer[2] = inputBuffer[3];
        outputBuffer[3] = inputBuffer[3];
        outputBuffer[4] = inputBuffer[40];
        outputBuffer[5] = inputBuffer[3];
        outputBuffer[6] = inputBuffer[27];
        outputBuffer[12] = inputBuffer[13];
        outputBuffer[13] = inputBuffer[39];
        outputBuffer[14] = inputBuffer[38] + " " +inputBuffer[40];
        //skipping outputBuffer[16] position 17 on spreadsheet
        outputBuffer[20] = inputBuffer[36];
        outputBuffer[22] = inputBuffer[37];
        outputBuffer[23] = inputBuffer[39];
        outputBuffer[24] = inputBuffer[40];
        outputBuffer[29] = inputBuffer[27];
        outputBuffer[33] = inputBuffer[18];
        outputBuffer[34] = inputBuffer[19];
        outputBuffer[38] = inputBuffer[39];
    }

    /*
     * This method uses the fields (array elements) in the output
     * buffer to assemble a CSV record (string variable). The
     * CSV record is then written to the output file.
     */
    public static void BuildRecordAndWriteOutput()
    {
        string record = "";

        for (int i = 0; i < outputBuffer.Length; i++)
        {
            if (outputBuffer[i].Contains(","))
            {
                string x = "\"" + outputBuffer[i] + "\"";
                record += x;
            }
            else
            {
                record += outputBuffer[i];
            }
            if (i < outputBuffer.Length - 1)
            {
                record += ",";
            }
        }

        output.WriteLine(record);
    }

    /*
     * This method retrieves information that has been organized and
     * placed into data structures. The information is then formatted,
     * placed into, and written to a CSV file.
     */
    public static void OrganizeDataAndCreateOutput()
    {
        while ()
        {
            SetOutputBufferDefaultValues(); // Put default values in the output buffer
            MapInputFieldsToOutputFields(); // Move fields from the input buffer to the output buffer.
            BuildRecordAndWriteOutput(); // Build record from output buffer and write it.
        }
    }
}

1 个答案:

答案 0 :(得分:0)

更新:阅读stuartd的评论后,看起来您希望input.EndOfData循环中的OrganizeDataAndCreateOutput()条件。所以你的程序看起来像是:

class Program
{
// File object variables
static TextFieldParser input = new TextFieldParser("PPVendingPricing.csv");
static StreamWriter output = new StreamWriter("convertedInventoryItemListTacMed.csv");

// Input and output buffer variables
static string[] inputBuffer;
static string[] outputBuffer = new string[40];


static void Main(string[] args)
{ 
    OrganizeDataAndCreateOutput();

    Console.WriteLine("done");
    input.Close();
    output.Close();
    Console.Read();
}

 /*
 * This method reads the input file and fill data structures
 * that are used to organize the data before moving selected
 * fields to the output buffer.
 */ 
public static void ReadInputAndBuildDataStructures()
{
    input.SetDelimiters(",");

    inputBuffer = input.ReadFields(); // Read a CSV record in to the inputBuffer. 
}

/*
* This method loads default values into the output
* buffer (string array). Some of these values will be 
* replaced before the output buffer is written to the file.
*/
public static void SetOutputBufferDefaultValues()
{
    // Initialize all fields to empty.
    for (int i = 0; i < outputBuffer.Length; i++)
    {
        outputBuffer[i] = "";
    }

    // Update selected fields with default values.
    outputBuffer[7] = "Solutions Inc";
    outputBuffer[10] = "TRUE";
    outputBuffer[11] = "FIFO";
    outputBuffer[15] = "TRUE";
    outputBuffer[17] = "Main";
    outputBuffer[19] = "TRUE";
    outputBuffer[21] = "Solutions Inc";
    outputBuffer[25] = "Main";
    outputBuffer[28] = "Periods of Supply";
    outputBuffer[32] = "1";
    outputBuffer[35] = "By Overall Item Qty";
    outputBuffer[36] = "TRUE";
    outputBuffer[37] = "TRUE";
}

/*
* This method maps selected values from the input buffer
* to the appropriate position in the output buffer.
*/
public static void MapInputFieldsToOutputFields()
{
    outputBuffer[0] = inputBuffer[26];
    outputBuffer[1] = inputBuffer[38];
    outputBuffer[2] = inputBuffer[3];
    outputBuffer[3] = inputBuffer[3];
    outputBuffer[4] = inputBuffer[40];
    outputBuffer[5] = inputBuffer[3];
    outputBuffer[6] = inputBuffer[27];
    outputBuffer[12] = inputBuffer[13];
    outputBuffer[13] = inputBuffer[39];
    outputBuffer[14] = inputBuffer[38] + " " +inputBuffer[40];
    //skipping outputBuffer[16] position 17 on spreadsheet
    outputBuffer[20] = inputBuffer[36];
    outputBuffer[22] = inputBuffer[37];
    outputBuffer[23] = inputBuffer[39];
    outputBuffer[24] = inputBuffer[40];
    outputBuffer[29] = inputBuffer[27];
    outputBuffer[33] = inputBuffer[18];
    outputBuffer[34] = inputBuffer[19];
    outputBuffer[38] = inputBuffer[39];
}

/*
 * This method uses the fields (array elements) in the output
 * buffer to assemble a CSV record (string variable). The
 * CSV record is then written to the output file.
 */
public static void BuildRecordAndWriteOutput()
{
    string record = "";

    for (int i = 0; i < outputBuffer.Length; i++)
    {
        if (outputBuffer[i].Contains(","))
        {
            string x = "\"" + outputBuffer[i] + "\"";
            record += x;
        }
        else
        {
            record += outputBuffer[i];
        }
        if (i < outputBuffer.Length - 1)
        {
            record += ",";
        }
    }

    output.WriteLine(record);
}

/*
 * This method retrieves information that has been organized and
 * placed into data structures. The information is then formatted,
 * placed into, and written to a CSV file.
 */
public static void OrganizeDataAndCreateOutput()
{
    input.ReadFields(); // Skip the header record.
    while (!input.EndOfData)
    {
        ReadInputAndBuildDataStructures();
        SetOutputBufferDefaultValues(); // Put default values in the output buffer
        MapInputFieldsToOutputFields(); // Move fields from the input buffer to the output buffer.
        BuildRecordAndWriteOutput(); // Build record from output buffer and write it.
    }
}
}