Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!helios!inetg1!news From: dedreb@arco.com (Richard Beecher) Newsgroups: comp.sys.mac.programmer Subject: Re: Numbers from TEXT files under Think C ? Message-ID: <1991Apr22.163030.17480@Arco.COM> Date: 22 Apr 91 16:30:30 GMT References: <17150002@hpihoah.cup.hp.com> Sender: news@Arco.COM Distribution: usa Organization: Arco Alaska Lines: 56 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 ---------------