[comp.sys.mac.programmer] writing a text file

kpmiller@uokmax.ecn.uoknor.edu (Kent P Miller) (04/24/91)

Ok.  Here's another one of my patented Stupid Questions (tm).

I need to pump a few lines of text to a file.  So, I'm looking through IM and
it says
FSWrite(refNum:integer; var count:longint; buffPtr:Ptr):OSErr;

I wouldn't know a buffPtr if it bit me on the butt.  What is it, and where can
I go to purchase one?

Thanks for your continued help.  

-- 
-----------------------
Kent Miller
KENT@aardvark.ucs.uoknor.edu
Bitnet -> KENT@uokucsvx

mxmora@unix.SRI.COM (Matt Mora) (04/25/91)

In article <1991Apr24.074449.1148@uokmax.ecn.uoknor.edu> kpmiller@uokmax.ecn.uoknor.edu (Kent P Miller) writes:
>Ok.  Here's another one of my patented Stupid Questions (tm).
>
>I need to pump a few lines of text to a file.  So, I'm looking through IM and
>it says
>FSWrite(refNum:integer; var count:longint; buffPtr:Ptr):OSErr;
>
>I wouldn't know a buffPtr if it bit me on the butt.  What is it, and where can
>I go to purchase one?

A "buffPtr" is a pointer to a buffer of data. If your data is in a pascal
string then you could do something like this:

count:=length(mystring);
error:=FSWrite(refNum, count, ptr(ord(@mystring)+1));

In the example above, we are passing the pointer of a string 1 byte passed
the length byte of the string.

If your data is/are in a TERecord then you could do this:

count:=MyTERec^^.teLength;	{set the length of the data}
error:=FSWrite(refNum, count, ptr(MyTERec^^.hText^));

That should write what was in MyTErec in to the file.





-- 
___________________________________________________________
Matthew Mora                |   my Mac  Matt_Mora@sri.com
SRI International           |  my unix  mxmora@unix.sri.com
___________________________________________________________

sdk4102@ritvax.isc.rit.edu (KNIGHT, SD) (04/25/91)

In article <1991Apr24.074449.1148@uokmax.ecn.uoknor.edu>, kpmiller@uokmax.ecn.uoknor.edu (Kent P Miller) writes...
>Ok.  Here's another one of my patented Stupid Questions (tm).
> 
>I need to pump a few lines of text to a file.  So, I'm looking through IM and
>it says
>FSWrite(refNum:integer; var count:longint; buffPtr:Ptr):OSErr;
> 
>I wouldn't know a buffPtr if it bit me on the butt.  What is it, and where can
>I go to purchase one?
> 
>Thanks for your continued help.  

  Well, while Ptr's have been known to nibble on me, they rarely bite (but when
they do!!!).  Any pointer will do (Ptr = pointer).  Some examples:

	int ptr1[10], *ptr2;  long len;  Str255 str;
	/* some code */
	len = sizeof(int) * 10;  FSWrite (refNum, &len, ptr1);

	/* or */
	ptr2 = ptr1;
	len = sizeof(int) * 10;  FSWrite (refNum, &len, ptr2);

	/* or in your case */
	strcpy (str, "a line of text\ranother line of text");
	len = strlen(str);
	FSWrite (refNum, &len, str+1);

  Please, cash only, and in small denominations.

> 
>-- 
>-----------------------
>Kent Miller
>KENT@aardvark.ucs.uoknor.edu
>Bitnet -> KENT@uokucsvx

steve knight

peirce@outpost.UUCP (Michael Peirce) (04/25/91)

In article <1991Apr24.074449.1148@uokmax.ecn.uoknor.edu>, kpmiller@uokmax.ecn.uoknor.edu (Kent P Miller) writes:
> 
> Ok.  Here's another one of my patented Stupid Questions (tm).
> 
> I need to pump a few lines of text to a file.  So, I'm looking through IM and
> it says
> FSWrite(refNum:integer; var count:longint; buffPtr:Ptr):OSErr;
> 
> I wouldn't know a buffPtr if it bit me on the butt.  What is it, and where can
> I go to purchase one?

A bufPtr just points to the data you want to write, the count tells
it how many bytes to find at that address.  For example (off the top
of my head):

PROCEDURE WriteAString(refNum : integer; theString : str255);
VAR
	howMuch	: longint;
	dataPtr	: Ptr;
BEGIN
	howMuch := LENGTH(theString);
	dataPtr := Ptr(LONGINT(@theString)+1);
	IF FSWrite(refNum,howMuch,dataPtr) <> noErr
		THEN ReportError;
END;

Notice that you have to point one beyond the string's address to skip
over the pascal length byte.

Also, count is a VAR parameter because it returns the number of bytes
that were actually written to the file.

You also might want to send CR's out after each string...

-- michael

--  Michael Peirce         --   outpost!peirce@claris.com
--  Peirce Software        --   Suite 301, 719 Hibiscus Place
--  Macintosh Programming  --   San Jose, California 95117
--           & Consulting  --   (408) 244-6554, AppleLink: PEIRCE

Lawson.English@p88.f15.n300.z1.fidonet.org (Lawson English) (05/01/91)

Matt Mora writes in a message to All

MM> count:=MyTERec^^.teLength; {set the length of the data} error:=FSWrite(refNum,-
MM>  count, ptr(MyTERec^^.hText^)); 
MM> That should write what was in MyTErec in to the file.

As I recall, you might get bit if you have set myTErec^^.hText without calling
TECalText first as teLength gets set according to GetHandleSize(myTErec^^.hText).


Lawson
 

--  
Uucp: ...{gatech,ames,rutgers}!ncar!asuvax!stjhmc!300!15.88!Lawson.English
Internet: Lawson.English@p88.f15.n300.z1.fidonet.org