[comp.sys.mac.programmer] i.o. problem with MPW C

keith@uw-apl.UUCP (Keith Kerr) (03/29/89)

I've just started using MPW C, and I'm having a problem with
very simple i/o.  I've tried several variations of the following
program (which runs fine on UNIX), but without success.  The
output file gets created, but upon completion it is either
empty, or unreadable.  Any pointers as to what I'm doing 
wrong would be helpful.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - --

#include <stdio.h> /* for buffered file output */
   
   



int main() /* prints out data to check it */
{
  FILE *out_file, *fopen();
  char *filename = "HD-0:MPW:HistoC:check_file_out";
  int how_much = 64, index, ok;
  
  out_file = fopen(filename, "w");
  if (out_file == NULL)
    {
	  /*SysBeep(5);*/
	  return;
	}
  else
    for (index = 1; index <= how_much; ++index)
      fprintf(out_file, "This shoulda been %d\n", index); 
		
  ok = fclose(out_file); 
  if (ok == (-1))
    {
	  return;
	  /*SysBeep(5);*/
	  /*SysBeep(5);*/
	}
  return(0);
}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
I've tried this with no type dec. for main, with no return,
with no explicit close, etc.  H E L P!!

levin@bbn.com (Joel B Levin) (03/29/89)

I took your entire article, commented out the header and .sig, and
changed the file name string to an absolute name that would work on my
disk.  The file, called oops.c was then compiled and linked as follows
(The "6" at the end of the lines in the link command was the option-d
line continuation character):

===================================
#  ** Compile and link: **

c oops.c -o oops.c.o
Link -w -c 'MPS ' -t MPST 6
	oops.c.o 6
	"{Libraries}"stubs.o 6
	"{CLibraries}"CRuntime.o 6
	"{Libraries}"Interface.o 6
	"{CLibraries}"StdCLib.o 6
	"{CLibraries}"CInterface.o 6
	-o oops

#	  return;
### Warning 269 This function has an explicit return type and deserves a return
value
    File "oops.c"; Line 34
#--------------------------------------------------------------------------
#	  return;
### Warning 269 This function has an explicit return type and deserves a return
value
    File "oops.c"; Line 43
#--------------------------------------------------------------------------

#  ** End of compile-and-link **
#  ** Run the tool **
oops

#  ** Look at the output: **

files -l check_file_out
Name                  Type Crtr  Size    Flags      Last-Mod-Date 
--------------------  ---- ---- ------ ---------- -----------------

check_file_out                      2K lvbspoimad  3/28/89  4:47 PM


catenate check_file_out
This shoulda been 1
This shoulda been 2
 . . .
This shoulda been 63
This shoulda been 64

===================================

Worked for me.  (MPW 3.0 release version)
UUCP:     {backbone}!bbn!levin		POTS: (617) 873-3463
INTERNET: levin@bbn.com

rick@Jessica.stanford.edu (Rick Wong) (03/29/89)

In article <222@uw-apl.UUCP> keith@uw-apl.UUCP (Keith Kerr) writes:
>I've just started using MPW C, and I'm having a problem with
>very simple i/o.  I've tried several variations of the following
>program (which runs fine on UNIX), but without success.  The
>output file gets created, but upon completion it is either
>empty, or unreadable.  Any pointers as to what I'm doing 
>wrong would be helpful.
>

    [simple i/o program deleted]

Your program is indeed writing what you expect to the file.  The only
problem is that the Finder doesn't know it's a text file (and so it
appears unreadable to you).  All you have to do is execute the following
command to the shell after running your program:

    setfile -t 'TEXT' -c 'MPS ' "your file name" <cr> <backspace> <enter>

As far as I know, there's no "clean" way to set a file's type from within
your program (you have to use some File Manager calls (eeahgghhgh)).

Rick Wong
Stanford University
rick@jessica.stanford.edu

earleh@eleazar.dartmouth.edu (Earle R. Horton) (03/29/89)

In article <222@uw-apl.UUCP> keith@uw-apl.UUCP (Keith Kerr) writes:
>I've just started using MPW C, and I'm having a problem with
>very simple i/o.  I've tried several variations of the following
>program (which runs fine on UNIX), but without success.  The
>output file gets created, but upon completion it is either
>empty, or unreadable.  Any pointers as to what I'm doing 
>wrong would be helpful.

     When you "fopen()" a file for writing with MPW C, it has the
default creator '    ' and default type '    ' unless you arrange for
it to have something else.  There wasn't anything wrong with this
program, except that it created its output file to be a '    ' file
instead of a 'TEXT' file, and your editor probably wouldn't open it.

     When using stdio to create files on the Mac with MPW C, you have
to set the file type and creator, as below.

------------------------------------------------------------------
#include <stdio.h> /* for buffered file output */

int main() /* prints out data to check it */
{
  FILE *out_file, *fopen();
  char *filename = "check_file_out";
  int how_much = 64, index, ok;
  
  Create(filename,0,'MPS ','TEXT');	/* So a text editor will open it. */
  out_file = fopen(filename, "w");
  if (out_file == NULL)
    {
	  /*SysBeep(5);*/
	  return;
	}
  else
    for (index = 1; index <= how_much; ++index)
      fprintf(out_file, "This shoulda been %d\n", index); 
		
  ok = fclose(out_file); 
  if (ok == (-1))
    {
	  return;
	  /*SysBeep(5);*/
	  /*SysBeep(5);*/
	}
  return(0);
}

earleh:xyzzy:32768:7:Earle R. Horton,,,6434109:/hackers/earleh:/bin/rn