簡體   English   中英

僅使用指針在動態結構中具有2d數組的fscanf

[英]fscanf with 2d array within dynamic struct using only pointers

因此,本次練習是我第一次使用C ++來自指針。 我的教授希望我們在不使用索引的情況下進行此練習以更好地掌握指針。 我正在嘗試從文件中讀取嵌入動態分配的結構中的二維數組。 我希望我已經很好地表達了我的問題,並提供了適當的信息。 非常感謝你。

我的結構

typedef struct  //struct that holds ASCII art
{
    char artistName[80];    //name of artist
    char asciiArt[20][80];  //actual ascii art line by line
    int rating;             //rating of art
}TextArt;

結構聲明和分配數組

TextArt **artPtr;
artPtr = malloc(1000 * sizeof(TextArt) + 1);

代碼行給我帶來了問題。 我從文件讀取到現在為止,我不確定這是否有效,我在第一個字符中尋找#。 不使用索引的語法對我來說非常令人困惑,因此這是我需要最大幫助的地方。 它是指向struct(* asciiArt + i)數組的指針,然后指向struct-> 2d c字符串數組asciiArt [] []。

        while(*tempLine != '#')   //while there is no # in position [0] -- read into temp string
        {
            printf("while templine\n");
            fgets((*asciiArt+i)->asciiArt, 100, pFile);  
        }

如果您需要更多信息,請使用上述代碼的完整功能,否則請忽略此代碼塊。

void readFromFile(TextArt **asciiArt, int size, char *fileName)
{
    int index = 0; //index for array of struct
    int i = 0;
    int j = 0;
    char tempLine[500]; //temp placeholder
    FILE *pFile;


    pFile = fopen (fileName ,"r");
    if (pFile == NULL)
        printf("Error opening file");
    else
    {printf("file opened.");
        for(i = 0; pFile != NULL; i++) //*(*(data + i) + j)
        {   j=0;
            fgets((*asciiArt+i)->artistName, 100, pFile); //get artist name from first line
            printf("got artist name");

            while(*tempLine != '#')
            {printf("while templine\n");
                fgets((*asciiArt+i)->asciiArt, 100, pFile);  //while there is no # -- read into temp string
            }
            strcpy(tempLine, ""); //clear temp string
            printf("for loop done");
        }
    return;
    }
}

這是個錯誤:

TextArt **artPtr;
artPtr = malloc(1000 * sizeof(TextArt) + 1);

sizeof(TextArt)sizeof(TextArt *)無關。 artPtr指向一個指針數組 ,而不是一個TextArt對象數組。 您剛剛分配(或嘗試分配)的那1000個指針中的每一個當前均指向無處,在使用它們之前,您必須將每個指針指向某處。

更新 :OP闡明了分配1000個TextArt數組的意圖:

TextArt *artPtr;
artPtr = malloc(1000 * sizeof(TextArt));

我不確定+ 1是什么意思。

如果readFromFile函數需要調整數組大小,則可以像這樣傳遞它:

void readFromFile( &artPtr, ......

在函數內部,您可以像這樣訪問它:

fgets((*asciiArt)[i].artistName, 100, pFile);

其實我會寫

TextArt *artPtr = *asciiArt;
fgets( artPtr[i].artistName, 100, pFile );

因為它更容易閱讀,而不是到處都有括號。

如果該函數不需要重新分配,則只需使用TextArt *artPtr;

大概

TextArt *artPtr;
artPtr = malloc(1000 * sizeof(TextArt));
...
void readFromFile(TextArt *asciiArt, int size, char *fileName){
    int index, i, ch; //index for array of struct
    char tempLine[500];
    FILE *pFile;

    pFile = fopen (fileName ,"r");
    if (pFile == NULL)
        printf("Error opening file");
    else{
        printf("file opened.");
        for(index = 0; index < size; ++index){
            TextArt *pta = asciiArt + index;
            fgets(pta->artistName, sizeof(pta->artistName), pFile);
            printf("got artist name");

            for(i=0;i<20;++i){//20 : sizeof(pta->asciiArt)/sizeof(pta->asciiArt[0])
                if(EOF==(ch=fgetc(pFile))){
                    index = size;//outer loop break
                    break;
                } else if(ch == '#'){
                    //ungetc(ch, pFile);
                    fgets(tempLine, sizeof(tempLine), pFile);
                    break;
                } else {
                    ungetc(ch, pFile);
                    fgets(pta->asciiArt + i, sizeof(pta->asciiArt[0]), pFile);
                }
            }
        }
        fclose(pFile);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM