fopen()

5 posts / 0 new
Last post
JosDuchIt
JosDuchIt's picture
Offline
Last seen: 8 years 1 week ago
Joined: 2012-01-08 20:57
fopen()

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

  1. int main ( int argc, char **argv )
  2. {
  3. FILE *ListFP = NULL;
  4. STRPTR TargetFName, OutputFile, ListFileName;
  5. int rval = RETURN_OK, i = 0;
  6.  
  7. // -----------------------------------------------------------------
  8. struct RDArgs *rdargs;
  9. LONG args[] = { 0, 0, 0};
  10. rdargs = ReadArgs("LISTFILE/A,OUTPUT/A, FUNCTION/A", args, NULL);
  11. if (argc != 4) /// was 3 , one more ?
  12. {
  13. fprintf( stderr, "usage LISTFILE/A,OUTPUT/A, FUNCTION/A \n");
  14.  
  15. return( RETURN_ERROR );
  16. }
  17. if ((int32 *)args[0] != NULL) {ListFileName = (STRPTR)args[0];}
  18. if ((int32 *)args[1] != NULL) {OutputFile = (STRPTR)args[1];}
  19. if ((int32 *)args[2] != NULL) {TargetFName = (STRPTR)args[2];}
  20. FreeArgs (rdargs);
  21. if (!(ListFP = fopen( ListFileName, "r" )))
  22. {
  23. rval = IoErr();
  24. printf("main Opening file ListFileName %s\n",ListFileName); /// not seen
  25. fprintf( stderr, "problem opening %s\n",
  26. ListFileName
  27. );
  28.  
  29. goto ExitCallScan;
  30. }
  31.  
  32. ExitCallScan:
  33. if (ListFP)/// (closeListfile == TRUE)
  34. fclose( ListFP );
  35. return( rval );
  36. }
thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
You use ListFileName after

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.

JosDuchIt
JosDuchIt's picture
Offline
Last seen: 8 years 1 week ago
Joined: 2012-01-08 20:57
@Thomas Thanks, works ok now

@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];}
...

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
What I meant is this: if

What I meant is this:

  1. if (argc != 4) /// was 3 , one more ?
  2. {
  3. fprintf( stderr, "usage LISTFILE/A,OUTPUT/A, FUNCTION/A \n");
  4.  
  5. return( RETURN_ERROR );
  6. }

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.

JosDuchIt
JosDuchIt's picture
Offline
Last seen: 8 years 1 week ago
Joined: 2012-01-08 20:57
@Thomas That was very

@Thomas
That was very clear.
Thanks for taking the trouble to explain

Log in or register to post comments