View Single Post
Old 13 January 2021, 23:06   #1
leemmcc
Registered User

 
Join Date: Nov 2020
Location: Barnsley
Posts: 8
Printf, STRPTR, and structs

He all, I've been a developer for years but first time using c in Amiga 68k. I'm using gcc.

I've got a struct and an pointer array of said struct.

Code:
#define MAXFILECOUNT 255

struct File {
	STRPTR fileName;
	LONG size;
};

struct File *files[MAXFILECOUNT];
And I've got a function to read a folder of files.

Code:
int clear(struct FileInfoBlock *fib, BPTR lock)
{
    if (fib != NULL){ FreeDosObject(DOS_FIB, fib); }
    if (lock != NULL){ UnLock(lock); }

    return RETURN_ERROR;
}

int setFiles(STRPTR path)
{
    struct File *fl;

    struct FileInfoBlock *fib = NULL;
    BPTR lock = Lock(path, ACCESS_READ);

	int a = 0;

    /**
     * Ensure no process already has a lock on the path
     */
    if (lock)
    {

        /**
         * Create instance of struct FileInfoBlock
         */
        fib = (struct FileInfoBlock *)AllocDosObject(DOS_FIB, NULL);
        if (!fib)
        { 
            Printf("Unable to allocate file information block.\n");
            return clear(fib, lock);
        } 
        else 
        {

            /**
             * Ensure we can examine the path
             */
            if (Examine(lock, fib))
            {

                /**
                 * Get each file in path, ignoring other directories 
                 */
                int idx = 0;
                while (ExNext(lock, fib))
                {
                    if (!(fib->fib_DirEntryType > 0))
                    {
						Printf("%20s\n", fib->fib_FileName);

                        fl = (struct File*) malloc(sizeof(struct File));
						fl->fileName = (STRPTR)fib->fib_FileName;
						fl->size = fib->fib_Size;

                        files[idx] = (struct File*)fl;
                        idx++;
                    }
                }

                clear(fib, lock);

            }
            else
            {
                Printf("Unable to examine the specified path.\n");
                return clear(fib, lock);
            }
            
        }
    } 
    else 
    {
        Printf("Unable to acquire a read lock to the specified path.\n");
        return clear(fib, lock);
    }

	
        // Temp code
	for (a = 0; a < MAXFILECOUNT; a++)
	{
		if (files[a]->size > 0){
			Printf("Filename %s\n", files[a]->fileName);
			Printf("Filesize %ld\n", files[a]->size);
		}
		else 
		{
			break;
		}
	}

    return RETURN_OK;
}
For each file I want to create a pointer of struct File and add it to the files array.

I get no complaint from the compiler whatsoever BUT when I Printf the added files (in the Temp code at the bottom of the listing), the printed Filesize is correct but Filename is corrupted.

Clearly I'm doing something wrong here. Any advice would be helpful.

Thanks!
leemmcc is offline  
 
Page generated in 0.04246 seconds with 11 queries