[comp.sys.mac.programmer] Numbers from TEXT files under Think C ?

cring@hpihoah.cup.hp.com (Craig Ring) (04/19/91)

I am writing a program in Think C 4.0 that needs to read numeric input from
an ASCII text file.  Under UNIX this is very easy to do with fscanf() and so
on.  Is there a simple way to do this with a Macintosh?  

I would like to be able to use SFGetFile() to allow the user to pick the
file they would like to open, but the SFReply doesn't seem to be very useful
to the ANSI C libraries for Think C.  The Think C documentation says that
scanf() requires a pointer to type FILE, which I am not sure how to get.

I could of course write my own routines to use the Toolbox calls to read
data from the file, and then convert the ASCII character codes to a numeric
data type, but this seems like a lot of work compared to using library
routines to do it for me.

Any help you can give me would be greatly appreciated.

Craig Ring
cring@leland.stanford.edu

marty@cernvax.cern.ch (hugues marty) (04/22/91)

In article <17150002@hpihoah.cup.hp.com> cring@hpihoah.cup.hp.com (Craig Ring) writes:
>I am writing a program in Think C 4.0 that needs to read numeric input from
>an ASCII text file.  Under UNIX this is very easy to do with fscanf() and so
>on.  Is there a simple way to do this with a Macintosh?  
>
>I would like to be able to use SFGetFile() to allow the user to pick the
>file they would like to open, but the SFReply doesn't seem to be very useful
>to the ANSI C libraries for Think C.  The Think C documentation says that
>scanf() requires a pointer to type FILE, which I am not sure how to get.

One way is to call SFGetFile(), then geting the whole path name
for the selected file (for example "HD:folder 1:document") and passing
this string to the fopen() function which will return a FILE *.
The problem is how to get the file path... and I don't have
a sample code off-hand. If you have access to technical notes,
this is documented in TN 238 (Getting a full pathname).

Good luck, Hugues.
-- 
e+ ----> :-) <---- e-                         e-mail :
Les Chercheurs de Bosons Unifies              marty@cernvax.cern.ch

chou@steelhead.cs.washington.edu (Pai Hsiang Chou) (04/22/91)

In article <4978@cernvax.cern.ch> marty@cernvax.cern.ch (hugues marty) writes:
>In article <17150002@hpihoah.cup.hp.com> cring@hpihoah.cup.hp.com (Craig Ring) writes:
>>I am writing a program in Think C 4.0 that needs to read numeric input from
>>an ASCII text file.  Under UNIX this is very easy to do with fscanf() and so
>>on.  Is there a simple way to do this with a Macintosh?  
>>
>>I would like to be able to use SFGetFile() to allow the user to pick the
>>file they would like to open, but the SFReply doesn't seem to be very useful
>>to the ANSI C libraries for Think C.  The Think C documentation says that
>>scanf() requires a pointer to type FILE, which I am not sure how to get.

How about fopen() with the filename?

>One way is to call SFGetFile(), then geting the whole path name
>for the selected file (for example "HD:folder 1:document") and passing
>this string to the fopen() function which will return a FILE *.
>The problem is how to get the file path... and I don't have
>a sample code off-hand. If you have access to technical notes,
>this is documented in TN 238 (Getting a full pathname).

No, it's not a good idea (and not necessary) to use the full path name.
There is a much simpler way.

	FILE *f;
	/* your SFGetFile() or SFPutFile() stuff */
	if (sfreply.good) {
		char name[64];
		SetVol(NULL,sfreply.vRefNum); /* set correct directory */

		/* some procedure which copies a Pascal str to a C str */
		PasToCStr(sfreply.fName, name);

		f = fopen(name, "r");
		...

Pai Chou
chou@june.cs.washington.edu

dedreb@arco.com (Richard Beecher) (04/22/91)

In article <17150002@hpihoah.cup.hp.com> cring@hpihoah.cup.hp.com (Craig Ring)
writes:
>I am writing a program in Think C 4.0 that needs to read numeric input from
>an ASCII text file.  Under UNIX this is very easy to do with fscanf() and so
>on.  Is there a simple way to do this with a Macintosh?  
>
>I would like to be able to use SFGetFile() to allow the user to pick the
>file they would like to open, but the SFReply doesn't seem to be very useful
>to the ANSI C libraries for Think C.  The Think C documentation says that
>scanf() requires a pointer to type FILE, which I am not sure how to get.
>
>I could of course write my own routines to use the Toolbox calls to read
>data from the file, and then convert the ASCII character codes to a numeric
>data type, but this seems like a lot of work compared to using library
>routines to do it for me.
>
>Any help you can give me would be greatly appreciated.
>
>Craig Ring
>cring@leland.stanford.edu
>

First of all, you might want to check out Technical Note #246, entitled "Mixing
HFS and C File I/O".  

I like to use the standard C functions to do a lot of my I/O, too.  Here's some
pseudo code that demonstrates the approach:


GetVol( NULL, &curVRefNum );        /* save the vRefNum */

SFGetFile( loc, etc....., reply );
if ( !reply.good ) break;           /* or return, or whatever */
SetVol( NULL,reply.vRefNum );

PtoCstr( (char *) &reply.fName );
filePtr = fopen( (char *) reply.fName, "r" );
if ( filePtr == NULL ) do whatever....
.
.
read from the file, or whatever
using C libraries (fscanf, fgets, etc.)
.
.
fclose( filePtr );
SetVol( NULL, curVRefNum );        /* restore the previous vRefNum */


Setting the default volume with SetVol( NULL, reply.vRefNum ) allows you to use
the standard C functions to open the file (with fopen).  Good luck, and don't
trust my syntax above! :-)

---------------
Richard Beecher
dedreb@arco.com
---------------

phils@chaos.cs.brandeis.edu (Phil Shapiro) (04/26/91)

In article <4978@cernvax.cern.ch> marty@cernvax.cern.ch (hugues marty) writes:
   In article <17150002@hpihoah.cup.hp.com> cring@hpihoah.cup.hp.com (Craig Ring) writes:
   >I would like to be able to use SFGetFile() to allow the user to pick the
   >file they would like to open, but the SFReply doesn't seem to be very useful
   >to the ANSI C libraries for Think C.

   One way is to call SFGetFile(), then geting the whole path name
   for the selected file (for example "HD:folder 1:document") and passing
   this string to the fopen() function which will return a FILE *.

Yikes.  How about:

#include <stdio.h>
#include <console.h>
#include <StdFilePkg.h>
#include <pascal.h>

main()
{
    SFReply reply;
    FILE *infile;

    chide(stdout);       /* let console library initialize mac toolbox */
    SFGetFile(0x00300040, "", 0L, -1, 0L, 0L, &reply);
    if (reply.good) {    /* user didn't press cancel... */
        SetVol(0L, reply.vRefNum);   /* use vRefNum/wdRefNum in reply */
        infile = fopen(PtoCstr((char *)reply.fName), "r");
        /* ... */
    }
}

An even easier way would be to use ccommand(), which displays a dialog
that you can use to redirect input at runtime.

	-phil
--
   Phil Shapiro                           Technical Support Analyst
   Language Products Group                     Symantec Corporation
		Internet: phils@chaos.cs.brandeis.edu