动态结构的动态数组

时间:2018-11-30 13:20:41

标签: arrays dynamic struct

我需要读取CSV文件并将其存储在动态数组中以备将来使用。 该文件由几个“块”组成。每个块都有一个“标题行”和各种数量的组件行。标头和组成行中的字段数是固定的。

CSV文件示例:

HeaderField1, HeaderField2, HeaderField3, HeaderField4
Cpo1Field1, Cpo1Field2, Cpo1Field3, Cpo1Field4, Cpo1Field5, Cpo1Field6
Cpo2Field1, Cpo2Field2, Cpo2Field3, Cpo2Field4, Cpo2Field5, Cpo2Field6
Cpo3Field1, Cpo3Field2, Cpo3Field3, Cpo3Field4, Cpo3Field5, Cpo3Field6

HeaderField1, HeaderField2, HeaderField3, HeaderField4
Cpo1Field1, Cpo1Field2, Cpo1Field3, Cpo1Field4, Cpo1Field5, Cpo1Field6
Cpo2Field1, Cpo2Field2, Cpo2Field3, Cpo2Field4, Cpo2Field5, Cpo2Field6

HeaderField1, HeaderField2, HeaderField3, HeaderField4
Cpo1Field1, Cpo1Field2, Cpo1Field3, Cpo1Field4, Cpo1Field5, Cpo1Field6
Cpo2Field1, Cpo2Field2, Cpo2Field3, Cpo2Field4, Cpo2Field5, Cpo2Field6
Cpo3Field1, Cpo3Field2, Cpo3Field3, Cpo3Field4, Cpo3Field5, Cpo3Field6
Cpo4Field1, Cpo4Field2, Cpo4Field3, Cpo4Field4, Cpo4Field5, Cpo4Field6

结果应该是

job[0].HeaderField[0] = HeaderField1
job[0].HeaderField[1] = HeaderField2
job[0].HeaderField[2] = HeaderField3
job[0].HeaderField[3] = HeaderField4
job[0].Cpo[0].CpoField[0] = Cpo1Field1
job[0].Cpo[0].CpoField[1] = Cpo1Field2
job[0].Cpo[0].CpoField[2] = Cpo1Field3
job[0].Cpo[0].CpoField[3] = Cpo1Field4
job[0].Cpo[0].CpoField[4] = Cpo1Field5
job[0].Cpo[0].CpoField[5] = Cpo1Field6
job[0].Cpo[1].CpoField[0] = Cpo2Field1
job[0].Cpo[1].CpoField[1] = Cpo2Field2
job[0].Cpo[1].CpoField[2] = Cpo2Field3
job[0].Cpo[1].CpoField[3] = Cpo2Field4
job[0].Cpo[1].CpoField[4] = Cpo2Field5
job[0].Cpo[1].CpoField[5] = Cpo2Field6
job[0].Cpo[2].CpoField[0] = Cpo3Field1
job[0].Cpo[2].CpoField[1] = Cpo3Field2
job[0].Cpo[2].CpoField[2] = Cpo3Field3
job[0].Cpo[2].CpoField[3] = Cpo3Field4
job[0].Cpo[2].CpoField[4] = Cpo3Field5
job[0].Cpo[2].CpoField[5] = Cpo3Field6

job[1].HeaderField[0] = HeaderField1
job[1].HeaderField[1] = HeaderField2
job[1].HeaderField[2] = HeaderField3
job[1].HeaderField[3] = HeaderField4
job[1].Cpo[0].CpoField[0] = Cpo1Field1
job[1].Cpo[0].CpoField[1] = Cpo1Field2
job[1].Cpo[0].CpoField[2] = Cpo1Field3
job[1].Cpo[0].CpoField[3] = Cpo1Field4
job[1].Cpo[0].CpoField[4] = Cpo1Field5
job[1].Cpo[0].CpoField[5] = Cpo1Field6
job[1].Cpo[1].CpoField[0] = Cpo2Field1
job[1].Cpo[1].CpoField[1] = Cpo2Field2
job[1].Cpo[1].CpoField[2] = Cpo2Field3
job[1].Cpo[1].CpoField[3] = Cpo2Field4
job[1].Cpo[1].CpoField[4] = Cpo2Field5
job[1].Cpo[1].CpoField[5] = Cpo2Field6

我尝试了以下

typedef char cpoField[6][50];  // 6 fields à 50 chars

typedef struct {
  char  headerFld[4][50];  // 4 fields à 50 chars
  cpoField  **cpoLine;     // pointer array to cpoFields    
} job;

job *jobArray;

// memory for first job (jobArray[0])
jobArray = calloc(1, sizeof(job));

strcpy(jobArray[0].headerFld[0], "job[0] header field 1");
strcpy(jobArray[0].headerFld[1], "job[0] header field 2");
strcpy(jobArray[0].headerFld[2], "job[0] header field 3");
strcpy(jobArray[0].headerFld[3], "job[0] header field 4");

工作正常,但涉及组件行时,编译器对我不满意

jobArray[0].cpoLine = calloc(1, sizeof(cpoField*));
jobArray[0].cpoLine[0] = calloc(1, sizeof(cpoField));
jobArray[0].cpoLine = realloc(jobArray[0].cpoLine, 2 * sizeof(cpoField*));
jobArray[0].cpoLine[1] = calloc(1, sizeof(cpoField));
jobArray[0].cpoLine = realloc(jobArray[0].cpoLine, 3 * sizeof(cpoField*));
jobArray[0].cpoLine[2] = calloc(1, sizeof(cpoField));

/* here the compiler says "left of '.' is not struct/union */
strcpy(jobArray[0].cpoLine[0].cpoField[0], "0 CpoLine 0 Field 1");
strcpy(jobArray[0].cpoLine[0].cpoField[1], "0 CpoLine 0 Field 2");
strcpy(jobArray[0].cpoLine[0].cpoField[2], "0 CpoLine 0 Field 3");
strcpy(jobArray[0].cpoLine[0].cpoField[3], "0 CpoLine 0 Field 4");
strcpy(jobArray[0].cpoLine[0].cpoField[4], "0 CpoLine 0 Field 5");
strcpy(jobArray[0].cpoLine[0].cpoField[5], "0 CpoLine 0 Field 6");

我希望有人能以一种可行的方式做到这一点。

非常感谢 卡乐

0 个答案:

没有答案