2016-06-17 221 views
0

我已經做了一個小型的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

這真的很難看到所有這些代碼的問題。一個[最小,完整和可驗證的例子](http://stackoverflow.com/help/mcve)將幫助回答這個問題,或者在創建一個你會發現你的錯誤的過程中。 – stuartd

+0

除了FileHelpers和CSVHelper之外,還有很多CSV解析器庫,它們已經可以做到這一點以及更多了,我知道更多 – Plutonix

+0

@Plutonix。我只是這樣做來學習邏輯和練習:) – Ashton

回答

0

更新:閱讀stuartd的評論之後,它看起來像你想在OrganizeDataAndCreateOutput()循環您input.EndOfData條件。所以你的程序看起來像這樣:

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. 
    } 
} 
} 
+2

我不認爲這是問題。我認爲問題出現在'while(!input.EndOfData) {inputBuffer = input.ReadFields(); //讀入一個CSV記錄到inputBuffer。 }' – stuartd

+0

它似乎只拾取第一條記錄:/也許這是我的方法的佈局?也許我應該放置變量或調用其他方法?謝謝大家的幫助! – Ashton

+0

是的,我的例子中的代碼只會循環一次,因爲我只是展示最簡單的情況。如果添加'testConditional'所在循環的結束條件,那麼它應該適用於你。 –