/** Accept data from the keyboard and write it to a BASIC-like text file **/
/** another hand-thrown program from the D McSweeney kiln, copyright 1990 **/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void write_bstring(FILE *out, char *str)
{
int n;

n=strlen(str);

fputc('\0', out);
fputc(n, out);

str += --n;

while( n-- >= 0 && !ferror(out))
   fputc(*(str--), out);

}

int main()
{
FILE *out;

char bstring[256];
int n;

out=fopen("BStrings", "w");

if(!out){
   printf("can't open the file!\n");
   exit(-1);
   }

do{
   printf("Enter a string: ");
   n = -1;
   do{
      bstring[++n] = getchar();
      }while(bstring[n] != '\n');
   bstring[n]='\0';
   if(n > 0)
      write_bstring(out, bstring);
   }while(strlen(bstring) > 0);

fclose(out);
printf("Thank you and goodnight!");

}

