HOWTO: Create an ASCII Text Output File with Headers & Footers

ID: Q172853


The information in this article applies to:


SUMMARY

Attempting to route a report to an ASCII text file from FoxPro 2.x for MS- DOS using the command REPORT FORM <report name> TO FILE <file name>, creates an ASCII text file without printer control codes. However, attempting to route a report to an ASCII text file from FoxPro for Macintosh using the command REPORT FORM <report name> TO FILE <file name>, creates an ASCII text file with embedded printer control codes.

In FoxPro 2.x for Windows, the simplest way to create an ASCII text file from a report is to use the Generic/Text only printer driver within Windows. However, if for some reason you cannot install this printer driver, you can use the following sample code as a workaround. The sample code below creates a report programmatically and sends it to an ASCII text file.


MORE INFORMATION

If printer control codes are present in an ASCII text file, they can cause significant difficulties when you try to share the file with another application or user. The sample code below illustrates hot to use low-level file input and output (I/O) functions to create an ASCII text output file with headers and footers.

Sample Code


   *begin program
   m.usefile=sys(2004)              && get the start up directory
   * add a path to the clients file
   m.usefile=m.usefile+"sample\organize\dbfs\clients.dbf"
   USE (m.usefile)                  && use the clients table
   gnhandle=FCREATE('output.txt',0) && create an output file
   * set up some variables to track pagination
   gnpage=1                         && page count variable
   gnlines=0                        && line count variable
   GO TOP                           && go to the first record
   * Now loop  through the table
   DO WHILE !EOF()
      * If the number of lines is 0, put in a page header
      IF gnlines>=65
         gnlines=0                  && reset the line count to 0
         gnpage=gnpage+1            && increment the page count
      ENDIF
      DO CASE
      CASE gnlines=0
         =pagehead()                && procedure to write page header
         LOOP
      CASE gnlines=56
         =pagefoot()                && procedure to write page footer
         LOOP
      OTHERWISE
         gnlines=gnlines+1          && increment the line count
         * concatenate four character fields
         out_line=client_id+SPACE(3)+company+SPACE(3)+city+SPACE(3)+state
         =FPUTS(gnhandle,out_line)  && output the string to the ASCII file
         * FPUTS() automatically adds a carriage return & line feed
         * change fputs to fwrite if you want to control insertion of
         * the carriage return & line feed characters
         SKIP                       && skip to the next record
      ENDCASE
   ENDDO
   * If not at the end of the page, pad blank lines
   IF gnlines<56
      out_line=""
      FOR i=gnlines TO 56
         =FPUTS(gnhandle,out_line)
         gnlines=gnlines+1
      NEXT
      =pagefoot()                   && write the page footer
   ENDIF
   =FCLOSE(gnhandle)                && close the open output file
   RETURN

   PROCEDURE pagehead
   =spacer()                        && insert blank lines
   * Place  the title into the file
   out_line="This is Sample ASCII Text Output"
   gnlines=gnlines+1                && increment the line count
   =FPUTS(gnhandle,out_line)        && write the page header
   =spacer()                        && insert blank lines
   RETURN

   PROCEDURE pagefoot
   =spacer()                        && insert blank lines
   * Place the date and page number in the footer of the file
   out_line=DTOC(DATE())+SPACE(50)+"Page "+LTRIM(STR(gnpage))
   =FPUTS(gnhandle,out_line)        && write the page footer
   =spacer()                        && insert blank lines
   gnlines=0                        && reset the line count to 0
   gnpage=gnpage+1                  && increment the page count
   RETURN

   PROCEDURE spacer
   * Padding four lines in this loop
   out_line=""
   FOR i=1 TO 4
      gnlines=gnlines+1
      =FPUTS(gnhandle,out_line)
   NEXT
   RETURN
   *end of program file 


REFERENCES

For additional information on creating ASCII output files, please see the following articles in the Microsoft Knowledge Base:

Q139144 PRB: Can't Print a Plain, Unformatted Report to a File
Q121631 HOWTO: Print a Report to a File in FoxPro for Windows

Additional query words:


Keywords          : kbcode kbVFp FoxWin FxprgGeneral FxprintGeneral 
Version           : MACINTOSH:2.5b,2.5c,2.6a; WINDOWS:2.5,2.5a,2.5b,2.6,2.6a
Platform          : MACINTOSH WINDOWS 
Issue type        : kbhowto 

Last Reviewed: August 12, 1999