ascii-pic --> 2 dim.array



  • Hallo,

    mich würde interessieren wie man zum beispiel ein rechteckiges ascii-bild einliese und in einem 2 dimensionalem dynamischen array speichert....
    ginge das mit 2 forschleifen irgendwie?

    lg



  • Hallo

    Jau!

    Mfg.
    way



  • sosos schrieb:

    Hallo,

    mich würde interessieren wie man zum beispiel ein rechteckiges ascii-bild einliese und in einem 2 dimensionalem dynamischen array speichert....
    ginge das mit 2 forschleifen irgendwie?

    lg

    Du musst zuerst feststellen, wie gross das Bild ist - Etwa so (Ich bitte jetzt alle C++ Puritisten zu entschuldigen, es wird etwas sehr "C"):

    int GetCountCol (FILE   *pSource)
    {
            int    iCountCol = 0;
    
            rewind (pSource);
    
            while (getc (pSource) != '\n')
                    iCountCol++;
    
            return iCountCol;
    }
    
    int GetCountRow (FILE   *pSource)
    {
            int    iCountRow = 0;
            char   cSingle;
    
            rewind (pSource);
    
            do
            {
                    cSingle = getc (pSource);
                    if (cSingle == '\n')
                            iCountRow++;
            }
            while (cSingle != EOF)
    
            return iCountRow;
    }
    
    char** AllocArray (int    iCountCol,
                       int    iCountRow)
    {
            *pReturn = (char **) calloc (iCountCol_loc, sizeof (* char));
            if (*pReturn == NULL)
            {
                    fprintf (stderr, "Uebel!\n");
                    abort ();
            }
    
            for (int iLoop = 0; iLoop < iCountCol_loc, iLoop++)
            {
                    pReturn [iLoop] = (char *) calloc (iCountCol_loc, sizeof (char));
                    if (pReturn [iLoop] == NULL)
                    {
                             fprintf (stderr, "Uebel 2!\n");
                             abort ();
                    }
            }
    
            return pReturn;
    }
    
    char** ReadInCharArray (FILE   *pSource,
                            int    *iCountCol,
                            int    *iCountRow)
    {
            int      iCountCol_loc,
                     iCountRow_loc;
            char   **pReturn;
    
            iCountCol_loc = GetCountCol (pSource);
            iCountRow_loc = GetCountRow (pSource);
    
            pReturn = AllocArray (iCountCol_loc, iCountRow_loc);
    
            rewind (pSource);
    
            for (int iRow = 0; iRow < iCountRow_loc; iRow++)
                    fscanf (pSource,"%s", pReturn [iRow]);
    
            *iCountCol = iCountCol_loc;
            *iCountRow = iCountRow_loc;
    
            return pReturn;
    }
    

Anmelden zum Antworten