2012-10-30 25 views
0

我一直在遍地搜索,但是我能找到的所有東西都是初始化一個結構數組,而不是在另一個函數中。這是我的舊家庭作業。我的任務是從文本文件讀取命令,將它們存儲到一個數組中,並將這些命令傳送到名爲sketchpad的程序中。我實際上覺得我應該在另一個函數中初始化/重新初始化我的結構數組,因爲在文本文件中有一個結束命令(我認爲在結束命令之後沒有任何結果)。仔細一看,我忘了迎合這樣一個事實,即你可以連續閱讀兩個文本文件。還有其他方式可以做到這一點,老師向我們展示了迎合它的一種方法。但我想知道如何按照我的方式去做。你將如何調用一個函數來初始化/重新初始化一個結構數組並將這些值發送回它被調用的地方?我們用指針吧?我們將如何去做呢?使用一個單獨的函數初始化一個結構數組

這裏是我的代碼:

void fileParsing(FILE * fp, FILE* pipe) 
{ 
    /*defines and initializes the commands being searched and counted */ 
    int Figure =0; 
    int draw =0; 
    int End =0; 
    int printFigure =0; 
    int drawFigure =0; 
    int translate =0; 
    int child =0; 
    int comment =0; 
    int i, j; 

    struct figure array_figure[19]; //WTF!!!! WHY 19????? 
    //assigns absurd values to array to distinguish when to stop looking for coordinates 
    for(i=0; i < FIGURE_MAX; ++i) 
    { 
     memset(array_figure[i].name, 0, LINE_LEN); 
     for(j=0; j<POINTS_MAX; ++j) 
     { 
     array_figure[i].Coord[j].xcoor = fup_value; 
     array_figure[i].Coord[j].ycoor =fup_value; 
     } 
    } 
    //array_figure[15].name = "adsfsdf"; 

    //printf("\n%d",array_figure[0].Coord[0].xcoor); 
    int count = 0; 
    int count2 = 0; 
    int drawCount = 0; 
    int reset; 
    //if(reset = 0; reset < 256; reset++); 
    //strncpy(array_figure[count].name, "0", LINE_LEN); 

    //printf("about to go into a while loop\n"); 

    while(fgets(line, LINE_LEN, fp)!=NULL) 
    { 
     sscanf(line, "%s%s%d%d", command, name, &x, &y); 

     //looks for commands and organizes them accordingly 
     if(strncmp(command, "Figure", 6) == 0) 
     { 
     printf("Found a Figure command\n"); 
     sscanf(line, "%s%s%d%d", command, name, &x, &y); 

     originX = x; 
     originY = y; 

     //assigns name and initial point 
     strncpy(array_figure[count2].name, name, LINE_LEN); 
     array_figure[count2].Coord[count].xcoor = x; //change: count2 from '3' from array_figure[i] 
     array_figure[count2].Coord[count].ycoor = y; 
     //printf("%s: %lf, %lf\n", array_figure[count2].name, array_figure[count2].Coord[count].xcoor, array_figure[count2].Coord[count].ycoor); 

     count++; 
     //printf("%d\n", count); 

     //fprintf(output, "%d %d\n", x, y); //MIGHT BE NEEDED! 
     //originX = x; 
     //originY = y; 
     } 

     else if(strncmp(command, "draw", 5) == 0) 
     { 
     printf("Found a draw command\n"); 
     sscanf(line, "%s%d%d", command, &x, &y); 
     //strncpy(array_figure[count].name, "0", LINE_LEN); 

     //priFiguntf("%s\n", array_figure[2].name); 

     //creates a new origin coordinate 
     originX = array_figure[count2].Coord[count-1].xcoor; 
     originY = array_figure[count2].Coord[count-1].ycoor; 

     //assigns more coordinates 
     array_figure[count2].Coord[count].xcoor = originX + x; 
     array_figure[count2].Coord[count].ycoor = originY + y; 
     //printf("%lf, %lf\n", array_figure[count2].Coord[count].xcoor, array_figure[count2].Coord[count].ycoor); 
     //printf("%c\n", array_figure[count]); 

     count++; 
     //printf("%d\n", count); 
     } 

     else if(strncmp(command, "drawFigure", 10) == 0) 
     { 
     printf("Found a drawFigure command\n"); 
     sscanf(line,"%s%s\n", command, name); 
     //if statement; iterates through to with strncmp to find figure name to draw 

     //printf("%s\n", name); 
     drawFigureCount= 0; 
     //checks for which element matchs the specified name for drawing 
     while(
      strncmp(array_figure[drawFigureCount].name, name, LINE_LEN) != 0 && 
      drawFigureCount < FIGURE_MAX) 
      drawFigureCount++; 

     if(drawFigureCount < FIGURE_MAX) 
     { 
      //printf("\tstartingX/Y assign\n"); 
      startingX = array_figure[drawFigureCount].Coord[0].xcoor; 
      startingY = array_figure[drawFigureCount].Coord[0].ycoor; 
      //printf("\tdone - startingX/Y assign\n"); 
      //sends coordinates to sketchpad 
      for(i=1; i< POINTS_MAX; ++i) 
      { 
       if(array_figure[drawFigureCount].Coord[i].xcoor != fup_value && 
        array_figure[drawFigureCount].Coord[i].ycoor != fup_value) 
       { 
        //printf("\t\tdraw segment sent to output\n"); 
        fprintf(pipe, 
        "drawSegment %ld %ld %ld %ld\n", 
        lround(startingX), lround(startingY), 
        lround(array_figure[drawFigureCount].Coord[i].xcoor), 
        lround(array_figure[drawFigureCount].Coord[i].ycoor) 
        ); 
        //printf("\t\tdone - draw segment sent to output\n"); 

        //printf("\t\tstartingX/Y update\n"); 
        startingX = array_figure[drawFigureCount].Coord[i].xcoor; 
        startingY = array_figure[drawFigureCount].Coord[i].ycoor; 
        //printf("\t\tdone - startingX/Y update\n"); 

       } else break; 
      } 
      //printf("\tbroke out of the loop or the loop ended\n"); 
     } 
     } 

     //checks for "end" in the input file to know when to stop making coordinates for a figure 
     else if(strncmp(command, "End", 3) == 0) 
     { 
     printf("Found a End command\n"); 
     //strncpy(array_figure[count].name, command, LINE_LEN); 
     //printf("%s\n", array_figure[count].name); 

     count2++; 
     count = 0; 
     } 

     //organizes and prints coordinates to console 
     else if(strncmp(command, "printFigure", 11) == 0) 
     { 
     //printf("Print Figure %s\n", name); **UNCOMMENT** 
     while(strncmp(array_figure[printFigureCount].name, name, LINE_LEN) != 0 && 
      printFigureCount < FIGURE_MAX) 
      printFigureCount++; 

     if(printFigureCount < FIGURE_MAX) 
     { 
      for(i=0; i< POINTS_MAX; ++i) 
      { 
       if(array_figure[printFigureCount].Coord[i].xcoor != fup_value && 
        array_figure[printFigureCount].Coord[i].ycoor != fup_value) 
       { 
        //converts values to int for printing 
        int intConvertX = (int) array_figure[printFigureCount].Coord[i].xcoor; 
        int intConvertY = (int) array_figure[printFigureCount].Coord[i].ycoor; 
        //printf("%d %d\n", intConvertX, intConvertY); **UNCOMMENT** 
       } 
      } 
     } 
     } 

     else if(strncmp(command, "translate", 9) == 0) 
     { 
     printf("Found a Translate command\n"); 
     //translate++; 
     } 
     /*else if(strncmp(command, "clearScreen", 11) == 0) 
     { 
     sscanf(line, "%s%s", command, clear); 
     printf("%s\n", clear); 

     }*/ 
     else if(strncmp(command, "child", 5) == 0) 
     { 
     sscanf(line, "%s%s", command, clear); 
     printf("Found a child command\n"); 
     //printf("\t\t\tsending child\n"); 

     fprintf(pipe, "%s", &line[5]); 
     printf("passed fprintf\n"); 

     if(strncmp(clear, "clearScreen", 11) == 0) 
     { 
      printf("%s\n", clear); 
      fprintf(pipe, "%s", &clear[11]); 
      break; 
     } 

     //printf("\t\t\tdone -- sending child\n"); 
     } 
     else if(strncmp(command, "#", 1) == 0) 
     { 
     printf("Found a # command\n"); 
     //printf("%s", line); **COMMENT OUT** 
     } 
    } 

如果你把一個通知,只要它說"clearScreen"它會在文本文件中顯示爲"child clearScreen"因此if(strncmp(clear, "clearScreen", 11) == 0)。之後我會將其中斷,但是我想讓它調用另一個函數來重新初始化一個數組,所以我不必離開函數。

回答

0

初始化您使用malloc分配所需的內存。 malloc返回一個指針,所以你可以把它放在一個函數中。如果你想重新初始化,你可以釋放結構並重新執行。

喜歡的東西:

typedef struct mystruct { 
    // ... 
}MyStruct; 

MyStruct* getMyStruct() { 
    return (MyStruct*) malloc(sizeof(MyStruct)); 
} 

檢查這些鏈接,可能會有幫助。

http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/

http://www.cplusplus.com/reference/clibrary/cstring/memset/

2

取決於你的意思是 「初始化」 的東西。

如果你已經宣佈你的結構數組作爲

struct figure array_figure[19]; 

,你只是想設置的元素回缺省值的所有值,那麼你可以將數組傳遞給函數和更改它裏面的函數(技術上你將指針傳遞給第一個元素)。一種這樣的函數簽名可能是這個樣子:

void modify(struct figure *array_of_structs, unsigned int length_of_array) 

,並調用它,如下所示:

modify(array_figure, 19); // if 19 is the length, that is 

如果你想有一個全新的陣列,則必須通過的建議分配一個新的喬納森克魯茲。