![]() |
![]() |
![]() |
#1 |
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]; 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; } 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! |
![]() |
![]() |
#2 |
This cat is no more
Join Date: Dec 2004
Location: FRANCE
Age: 49
Posts: 5,104
|
Code:
fl->fileName = (STRPTR)fib->fib_FileName; change your structure so fileName is a char fileName[108] then you can strcpy(fl->fileName,(STRPTR)fib->fib_FileName) or malloc the exact string size (+1) and same string copy (needs deallocation in the end) |
![]() |
![]() |
#3 |
Registered User
Join Date: Jan 2002
Location: Germany
Posts: 6,191
|
The C language does not know strings. You cannot assign strings using the = operator in C.
A STRPTR is a pointer to an array of characters. By assigning fileName to fib_FileName you let the pointer point to a part of the memory you allocated using AllocDosObjects before. Later you free that memory so that fileName now points to memory which is no longer valid. What you have to do is fl->fileName = malloc(strlen(fib->fib_FileName)+1); strcpy (fl->fileName,fib->fib_FileName); |
![]() |
![]() |
#4 |
Registered User
![]() Join Date: Nov 2020
Location: Barnsley
Posts: 8
|
Of course that's what it is! Thanks so much.
This is what happens we you spend 20 years writing strongly typed languages with their own garbage collection and memory management... ![]() Thanks again jotd and thomas. Last edited by leemmcc; 15 January 2021 at 00:02. |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Why printf in hook function freezes everything? | Sim085 | Coders. C/C++ | 5 | 29 June 2020 20:39 |
How to create a 'Array of structs' in AMOS? | thyslo | Coders. AMOS | 7 | 11 May 2020 13:18 |
Integers vs floats (FFP/Sing/Doub) + printf() | guy lateur | Coders. Asm / Hardware | 63 | 18 July 2017 18:57 |
|
|