Why do i get unreadable printout with the fopen() function ?
The argumens are well recognised
i used a filename for a file in the same drawer, a fullpath, a fullpath embedde in double quotes.
Allways the same result
int main ( int argc, char **argv ) { FILE *ListFP = NULL; STRPTR TargetFName, OutputFile, ListFileName; int rval = RETURN_OK, i = 0; // ----------------------------------------------------------------- struct RDArgs *rdargs; LONG args[] = { 0, 0, 0}; rdargs = ReadArgs("LISTFILE/A,OUTPUT/A, FUNCTION/A", args, NULL); if (argc != 4) /// was 3 , one more ? { fprintf( stderr, "usage LISTFILE/A,OUTPUT/A, FUNCTION/A \n"); return( RETURN_ERROR ); } if ((int32 *)args[0] != NULL) {ListFileName = (STRPTR)args[0];} if ((int32 *)args[1] != NULL) {OutputFile = (STRPTR)args[1];} if ((int32 *)args[2] != NULL) {TargetFName = (STRPTR)args[2];} FreeArgs (rdargs); if (!(ListFP = fopen( ListFileName, "r" ))) { rval = IoErr(); printf("main Opening file ListFileName %s\n",ListFileName); /// not seen fprintf( stderr, "problem opening %s\n", ListFileName ); goto ExitCallScan; } ExitCallScan: if (ListFP)/// (closeListfile == TRUE) fclose( ListFP ); return( rval ); }
You use ListFileName after FreeArgs(). ListFileName is a copy of args[0] and args[0] is filled in by ReadArgs. After FreeArgs all memory allocated by ReadArgs is freed and so is the memory pointed to by args[0] a.k.a. ListFileName.
BTW. you don't check the result of ReadArgs. The contents of args[] is unpredictable if ReadArgs failed. Argc==4 is not an indication that ReadArgs succeeded. If you use ReadArgs you should refrain from using argc/argv at all.
@Thomas
Thanks, works ok now
as to the check of ReadArgs results
are the lines following the check of the nr of args not what has to be cone?
if ((int32 *)args[0] != NULL) {ListFileName = (STRPTR)args[0];}
...
What I meant is this:
Not only that argc does not necessarily correspond to the number of arguments given.
Imagine that the user enters something like this: LISTFILE ram:list OUTPUT ram:out FUNCTION ram:funct
Now argc is 6, but ReadArgs succeeds.
Or this: LISTFILE ram:list OUTPUT FUNCTION
Now argc is 4, but ReadArgs fails.
Checking if rdargs is set is the only valid way to check the function of ReadArgs.
And if ReadArgs succeeds, you don't need to check args[x] against NULL any more because ReadArgs only succeeds if all arguments are set (you put /A behind every argument which ensures this).
Finally, if ReadArgs failed, you should tell the user why with PrintFault.
@Thomas
That was very clear.
Thanks for taking the trouble to explain