Reading/loading a 'text' file as a subfile, or into a flat file - is there an easy way ?

Mike Godsey mgo at columbus.rr.com
Sat Oct 13 06:49:27 CDT 2012


Here is a C program for the wintel platform that converts a variable length
file to a fixed length:



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

#define  MAXLENGTH  10000       /* Maximum input record length */
#define  DEFLENGTH     80       /* Default record length       */

main(argc,argv)
  int    argc;
  char  *argv[];

{ int      length;           /* record length */
  FILE    *ifp, *ofp;        /* file pointers */

  char     line[MAXLENGTH];  /* input line buffer */
  char    *rem;              /* string pointer for conversion checking */
  int      i,j,k;            /* loop/subscript */
  int      linelen;          /* input line length */
  int      count;            /* record count */
  int      CR;               /* TRUE if user wants carriage-return
characters
                                added; FALSE otherwise */


  length = DEFLENGTH-1;      /* Record length excluding the delimiter \n */
  count  = 0;
  CR     = 0;                /* Default is not to add CR */
  ifp    = NULL;
  ofp    = NULL;


  /* Gather up the input arguments and open the files: */

  if (argc > 6)
    { fprintf (stderr,"Usage: fixlen [-c] [-r length] [ input [ output ]
]\n");
      exit(0);
    }

  i = 1;
  while (i < argc)
    { if (strcmp(argv[i],"-c") == 0)       /* Add CR option */
          CR = 1;

      else if (strcmp(argv[i],"-r") == 0)  /* Record length specified */
        { i++;
          length = strtol(argv[i],&rem,10) - 1;
          if (strcmp(rem,"")  ||  length < 1  ||  length > MAXLENGTH)
            { fprintf (stderr,"fixlen: Invalid record length
(%s).\n",argv[i]);
              exit(1);
            }
        }

      else if (ifp == NULL)                /* Input file name */
        { if ((ifp = fopen(argv[i],"r")) == NULL)
            { fprintf (stderr,"fixlen: Could not open %s for reading
(%s)\n",
                              argv[i],strerror(errno));
              exit(10);
            }
        }

      else                                 /* Output file name */
        { if ((ofp = fopen(argv[i],"w")) == NULL)
            { fprintf (stderr,"fixlen: Could not open %s for writing
(%s)\n",
                               argv[i],strerror(errno));
              exit(11);
            }
        }

      i++;     /* Next argument */
    }

  /* Use standard I/O if none was specified: */

  if (ifp == NULL)
      ifp = stdin;
  if (ofp == NULL)
      ofp = stdout;

  /* If we're outputting CRLF lines, we'll need an extra space reserved for
     the CR, so we drop the input length by one:
  */

  if (CR == 1) length--;


/*--------------------------------------------------------------------------
-*/

  /* Loop through the file records, truncating those which are too long,
     blank-padding those that are too short.  The record length includes
     the final carriage control character(s).
  */

  while (fgets(line,MAXLENGTH,ifp) !=NULL)
    { linelen = strlen(line);
      count++;

      /* We check the first line for carriage control.  If the file
currently
         uses CRLF, then we will use CRLF on output regardless of the value
         of the CR flag:
      */

      if (count == 1  &&  CR == 0)
        { if (line[linelen-2] == '\r')
            { CR = 1;
              length--;
            }
        }

      /* Check for '\n' character.  If it's not there the record length was
         exceeded:
      */

      if (line[linelen-1] != '\n')
        { fprintf (stderr,"fixlen: Maximum line length (%d) exceeded at ",
                           MAXLENGTH);
          fprintf (stderr," record %d.\n",count);
          exit(20);
        }

      /* Erase the current carriage-control by overwriting it with '\0'
         characters:
      */

      line[--linelen] = '\0';
      if (line[linelen-1] == '\r') line[--linelen] = '\0';


      /* Check for long lines: */

      if (linelen > length)
        { fprintf (stderr,"fixlen: output length (%d) exceeded at record
%d;",
                           length+1, count);
          fprintf (stderr,"truncated.\n");
        }

      /* Check for short lines: */

      else if (linelen < length)
        { for (i=linelen; i < length; ++i)  line[i] = ' ';
        }

      /* Mark the end of the string and write it: */

      if (CR)
        { line[length]   = '\r';
          line[length+1] = '\n';
          line[length+2] = '\0';
        }
      else
        { line[length]   = '\n';
          line[length+1] = '\0';
        }

      fputs(line,ofp);
    }

/*--------------------------------------------------------------------------
-*/

  /* Close files and leave: */

  if (ifp != stdin)  fclose(ifp);
  if (ofp != stdout) fclose(ofp);
  exit(0);
}

-----Original Message-----
From: powerh-l-bounces+mgo=columbus.rr.com at lists.sowder.com
[mailto:powerh-l-bounces+mgo=columbus.rr.com at lists.sowder.com] On Behalf Of
Richard Witkopp
Sent: Friday, October 12, 2012 4:06 PM
To: powerh-l at lists.sowder.com
Subject: RE: Reading/loading a 'text' file as a subfile, or into a flat file
- is there an easy way ?

On VMS I just write a quick (no pun intended) ad hoc program to create a
subfile that matches the format of the file you're trying to read. Then I
run an analyze on the subfile .sf to get an .fdl. Then I run CONVERT on the
text file using the .fdl, rename it to be the same as the subfile, and
you're done.

I imagine similar utilities are available on other platforms.

-----Original Message-----
From: powerh-l-bounces+rwitkopp=phxa.com at lists.sowder.com
[mailto:powerh-l-bounces+rwitkopp=phxa.com at lists.sowder.com] On Behalf Of
Michel Adam
Sent: Friday, October 12, 2012 12:34 PM
To: powerh-l at lists.sowder.com
Subject: Reading/loading a 'text' file as a subfile, or into a flat file -
is there an easy way ?

This has probably been answered any number of times before, but:

With PH on Wintel, I'd like to process a '.txt' file with QTP, or convert it
/ load it into a flat file with the least amount of fuss possible.

Are there any tools/tricks that will let PH deal with a variable record
length (byte stream) file as if it was a fixed length flat file ?

Thanks 

Michel Adam


--
= = = = = = = = = = = = = = = = = = = = = = = = = = = = Mailing list:
powerh-l at lists.sowder.com
Subscribe: 'subscribe' in message body to powerh-l-request at lists.sowder.com
Unsubscribe: 'unsubscribe &lt;password&gt;' in message body to
powerh-l-request at lists.sowder.com
http://lists.sowder.com/mailman/listinfo/powerh-l
This list is closed, thus to post to the list you must be a subscriber.
Add 'site:lists.sowder.com powerh-l' to your search terms to search the list
archive at Google.

----------------------------------------------------------------------------
--
NOTICE: The information contained in this e-mail and any attachments is
confidential and may be privileged or otherwise protected from
disclosure.This e-mail is intended solely for the use of the named
addressee. Any other use, printing, copying, disclosure or dissemination may
be subject to legal restriction. If you are not the intended recipient,
please contact the sender and delete all copies including any attachments.

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

--
= = = = = = = = = = = = = = = = = = = = = = = = = = = = Mailing list:
powerh-l at lists.sowder.com
Subscribe: 'subscribe' in message body to powerh-l-request at lists.sowder.com
Unsubscribe: 'unsubscribe &lt;password&gt;' in message body to
powerh-l-request at lists.sowder.com
http://lists.sowder.com/mailman/listinfo/powerh-l
This list is closed, thus to post to the list you must be a subscriber.
Add 'site:lists.sowder.com powerh-l' to your search terms to search the list
archive at Google.




More information about the powerh-l mailing list