home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / batch.lzh / READ.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-10-19  |  3.4 KB  |  93 lines

  1. /*    read.c
  2.  
  3.     This program impliments a read command for batch files.
  4.     What read actually does is re-open the file who's name was
  5.     stored in the environment string, and read the next line from
  6.     it.  The postion in the file is updated and stored back in
  7.     the environment string, so subiquent calls to read will return
  8.     successive lines from the text file.
  9.         The file gets opened over and over again for each line,
  10.     but you gain all the advantages of batch files with little
  11.     programming.
  12.     
  13.     To use open in a batch file type:
  14.     
  15.     READ <handle> <stringname>
  16.     
  17.     handle is the unique identifer you bave to OPEN.
  18.     stringname is the name of a string to store the text in the
  19.         environment.
  20.     
  21.     READ returns errorlevel=1 for failure, 0 for success
  22. */
  23.  
  24. #include <stdio.h>
  25.  
  26. extern char    *env;        /*pointer to start of environment string*/
  27. extern char    *tem;        /*temporary pointer for searching*/
  28. extern int    enl;        /*length of environment in bytes*/
  29.  
  30. extern char *search_for();    /*searches environment for string*/
  31. extern del_string();        /*deletes a string from the environment*/
  32. extern int ins_string();    /*insert a new string in the environment*/
  33.  
  34. char    name[128]="n_";    /*space for the file name string*/
  35. char    pos[64]="p_";    /*space for the file position string*/
  36. char    line[200];    /*space for the line read from file*/
  37.  
  38. main(argc,argv)        /*main part of read program*/
  39. int    argc;
  40. char    *argv[];
  41. {
  42.     int    arg=1;        /*where to look for arguments*/
  43.     char *ptr;        /*scratch pointer for scanning around*/
  44.     FILE *fh;        /*file handle to read with*/
  45.     long posit;        /*position in file*/
  46.     int    l;        /*scratch for string lengths*/
  47.  
  48.     if (argc<2)        /*do I have enough arguments?*/
  49.     {
  50.     printf("Usage: READ <handle> <pathname>\r\n");
  51.     exit(1);    /*exit if no file name*/
  52.     }
  53.     if (argc<3)        /*if there is only one argument,*/
  54.     arg=0;        /*use it as the file name*/
  55.     enl=getenv(&env);    /*get the environment string*/
  56.     strcat(name,argv[arg]);    /*build the file name string*/
  57.     strcat(name,"=");
  58.     ptr=search_for(name);    /*is there a file for this handle?*/
  59.     if (ptr == NULL)
  60.     {
  61.     printf("You did not OPEN a file first\r\n");
  62.     exit(1);
  63.     }
  64.     fh=fopen((ptr+=strlen(name)),"r");    /*open the file*/
  65.     if (fh == NULL)            /*abort if you cannot open it*/
  66.     {
  67.     printf("Cannot open file %s\r\n",ptr);
  68.     exit(1);
  69.     }
  70.     strcat(pos,argv[arg]);    /*build the file position string*/
  71.     strcat(pos,"=");
  72.     ptr=search_for(pos);    /*find address of position string*/
  73.     if (ptr == NULL)
  74.     {
  75.     printf("There is something seriously wrong in environment!\r\n");
  76.     exit(1);
  77.     }
  78.     sscanf(ptr+strlen(pos),"%ld",&posit);    /*get file position*/
  79.     fseek(fh,posit,0);                /*go there*/
  80.     strcpy(line,argv[arg+1]);        /*build the text string name*/
  81.     strcat(line,"=");
  82.     del_string(line);            /*deletae any old string same name*/
  83.     if (fgets(line+strlen(line),180,fh)== NULL) /*read a line of text*/
  84.     exit(1);        /*must have hit eof*/
  85.     del_string(pos);            /*delete the string position*/
  86.     posit=ftell(fh);            /*find out where we are in the file*/
  87.     sprintf(pos+strlen(pos),"%ld",posit);    /*now and convert to string*/
  88.     ins_string(pos);            /*store back in environment*/
  89.     line[strlen(line)-1]=0;        /*chop the newline off the end*/
  90.     ins_string(line);            /*add it to the environment*/
  91.     fclose(fh);
  92.     exit(0);
  93. }