2016-06-17 5 views
0

작은 CSV 변환 프로그램을 만들었고 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) 
    { 
     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

이 코드는 모두 문제가 발생했습니다. [Minimal, Complete, Verifiable example] (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(); // inputBuffer에 CSV 레코드를 읽습니다. }' – stuartd

+0

첫 번째 레코드 만 가져 오는 것 같습니다 :/아마도 내 방법의 레이아웃입니까? 어쩌면 변수를 배치하거나 다른 곳에서 메소드를 호출해야합니까? 도와 주셔서 감사합니다! – Ashton

+0

예 간단한 예제를 보여주기 때문에 예제의 코드는 한 번만 반복됩니다. 'testConditional'이있는 곳에 루프의 끝 조건을 추가하면, 그것은 당신을 위해 일해야합니다. –