A
Andrew Clark
Hello all,
Wow, has it ever been a long time since I posted anything to this
group. I don't keep as current as I used to anymore; I only lurk from
time to time when I have a spare moment.
I am trying to write a program to open a file and process it
according to command line parameters. All I need are two simple search
and replace operations. I invoke the program with an int representing a
site code and a long representing a date and my program should replace
some hard-coded text strings in the file with the int and the long I pass
in. I may have some insight as to why this is not working now, but I'd
like some constructive criticism here. I tried to conform to the standard
(as I know it), but, well, let's see how it goes... Thanks for reading!
Andrew
/* SiteUpdate.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 256
#define SITEMIN 0
#define SITEMAX 599
#define STARTDATE 20050401
#define ENDDATE 29991231
#define IN "SiteUpdate.sql"
int fgetline ( FILE *, char * );
int main ( int argc, char *argv[] )
{
int rc = EXIT_FAILURE;
int site_code = SITEMIN - 1;
long date = STARTDATE - 1;
if ( 3 == argc )
{
site_code = atoi ( argv[1] );
if ( site_code < SITEMIN || SITEMAX < site_code )
{
fprintf ( stderr, "Site code must be a number between %d and
%d.\n", SITEMIN, SITEMAX );
}
else
{
date = atol ( argv[2] );
if ( date < STARTDATE || ENDDATE < date )
{
fprintf ( stderr, "%s\n", "Date must be in the format
\"YYYYMMDD\" and between" );
fprintf ( stderr, "%s\n", "today and December 31,
2999." );
}
else
{
char *outfile, code[4];
FILE *in, *out;
sprintf ( code, "%d", site_code + 400 );
/* subtract 1 for "site" -> "xxx" and */
/* add one for NUL character */
outfile = malloc ( strlen ( IN ) - 1 + 1 );
if ( outfile )
{
sprintf ( outfile, "%sUpdate.sql", code );
in = fopen ( IN, "r" );
out = fopen ( outfile, "w+" );
if ( in && out )
{
char buf[BUFSIZE], *p;
char *temp;
while ( EOF != fgetline ( in, buf ) )
{
if ( buf == strstr ( buf, "--" ) )
{
/* If the line is a comment, do not */
/* include it in the final script */
continue;
}
else if ( ( p = strstr ( buf, "xxx" ) ) )
{
temp = malloc ( strlen ( buf ) + 1 );
if ( temp )
{
/* Replace "xxx" with site code */
strcpy ( temp, buf );
sprintf ( p, "%s", code );
strcat ( buf, strstr ( temp, "xxx" )
+ strlen ( "xxx" ) );
fprintf ( out, "%s\n", buf );
free ( temp );
temp = NULL;
}
else
{
fprintf ( stderr, "%s\n", "Not enough
memory." );
}
}
else if ( ( p = strstr ( buf, "yyyyyyyy" ) )
)
{
temp = malloc ( strlen ( buf ) + 1 );
if ( temp )
{
/* Replace "yyyyyyyy" with date */
strcpy ( temp, buf );
sprintf ( p, "%ld", date );
strcat ( buf, strstr ( temp,
"\\gen" ) );
fprintf ( out, "%s\n", buf );
free ( temp );
temp = NULL;
}
else
{
fprintf ( stderr, "%s\n", "Not enough
memory." );
}
}
else
{
/* Nothing to do, just copy the line */
fprintf ( out, "%s\n", buf );
}
}
fclose ( in );
fclose ( out );
rc = EXIT_SUCCESS;
}
else
{
fprintf ( stderr, "Could not open %s!\n", ( in ?
outfile : IN ) );
}
free ( outfile );
}
}
}
}
else
{
fprintf ( stderr, "\n" );
fprintf ( stderr, "Usage: %s <site_code> <date>\n", argv[0] );
fprintf ( stderr, "%s\n", " site_code two digit number
representing the site" );
fprintf ( stderr, "%s\n", " date eight digit date
in the format \"YYYYMMDD\"" );
fprintf ( stderr, "%c\n", '\n' );
fprintf ( stderr, "%s\n", "Generates a SQL script for the month.
Input file is .\\SiteUpdate.sql" );
}
return rc;
}
int fgetline ( FILE *fp, char *buf )
{
char *p;
int c, i = 0;
p = buf;
while ( EOF != ( c = fgetc ( fp ) ) && '\n' != c && i < BUFSIZE )
{
*p = c;
++p;
++i;
}
*p = 0;
if ( ( p = strchr ( buf, '\n' ) ) ) *p = 0;
return c;
}
/* end SiteUpdate.c */
Wow, has it ever been a long time since I posted anything to this
group. I don't keep as current as I used to anymore; I only lurk from
time to time when I have a spare moment.
I am trying to write a program to open a file and process it
according to command line parameters. All I need are two simple search
and replace operations. I invoke the program with an int representing a
site code and a long representing a date and my program should replace
some hard-coded text strings in the file with the int and the long I pass
in. I may have some insight as to why this is not working now, but I'd
like some constructive criticism here. I tried to conform to the standard
(as I know it), but, well, let's see how it goes... Thanks for reading!
Andrew
/* SiteUpdate.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 256
#define SITEMIN 0
#define SITEMAX 599
#define STARTDATE 20050401
#define ENDDATE 29991231
#define IN "SiteUpdate.sql"
int fgetline ( FILE *, char * );
int main ( int argc, char *argv[] )
{
int rc = EXIT_FAILURE;
int site_code = SITEMIN - 1;
long date = STARTDATE - 1;
if ( 3 == argc )
{
site_code = atoi ( argv[1] );
if ( site_code < SITEMIN || SITEMAX < site_code )
{
fprintf ( stderr, "Site code must be a number between %d and
%d.\n", SITEMIN, SITEMAX );
}
else
{
date = atol ( argv[2] );
if ( date < STARTDATE || ENDDATE < date )
{
fprintf ( stderr, "%s\n", "Date must be in the format
\"YYYYMMDD\" and between" );
fprintf ( stderr, "%s\n", "today and December 31,
2999." );
}
else
{
char *outfile, code[4];
FILE *in, *out;
sprintf ( code, "%d", site_code + 400 );
/* subtract 1 for "site" -> "xxx" and */
/* add one for NUL character */
outfile = malloc ( strlen ( IN ) - 1 + 1 );
if ( outfile )
{
sprintf ( outfile, "%sUpdate.sql", code );
in = fopen ( IN, "r" );
out = fopen ( outfile, "w+" );
if ( in && out )
{
char buf[BUFSIZE], *p;
char *temp;
while ( EOF != fgetline ( in, buf ) )
{
if ( buf == strstr ( buf, "--" ) )
{
/* If the line is a comment, do not */
/* include it in the final script */
continue;
}
else if ( ( p = strstr ( buf, "xxx" ) ) )
{
temp = malloc ( strlen ( buf ) + 1 );
if ( temp )
{
/* Replace "xxx" with site code */
strcpy ( temp, buf );
sprintf ( p, "%s", code );
strcat ( buf, strstr ( temp, "xxx" )
+ strlen ( "xxx" ) );
fprintf ( out, "%s\n", buf );
free ( temp );
temp = NULL;
}
else
{
fprintf ( stderr, "%s\n", "Not enough
memory." );
}
}
else if ( ( p = strstr ( buf, "yyyyyyyy" ) )
)
{
temp = malloc ( strlen ( buf ) + 1 );
if ( temp )
{
/* Replace "yyyyyyyy" with date */
strcpy ( temp, buf );
sprintf ( p, "%ld", date );
strcat ( buf, strstr ( temp,
"\\gen" ) );
fprintf ( out, "%s\n", buf );
free ( temp );
temp = NULL;
}
else
{
fprintf ( stderr, "%s\n", "Not enough
memory." );
}
}
else
{
/* Nothing to do, just copy the line */
fprintf ( out, "%s\n", buf );
}
}
fclose ( in );
fclose ( out );
rc = EXIT_SUCCESS;
}
else
{
fprintf ( stderr, "Could not open %s!\n", ( in ?
outfile : IN ) );
}
free ( outfile );
}
}
}
}
else
{
fprintf ( stderr, "\n" );
fprintf ( stderr, "Usage: %s <site_code> <date>\n", argv[0] );
fprintf ( stderr, "%s\n", " site_code two digit number
representing the site" );
fprintf ( stderr, "%s\n", " date eight digit date
in the format \"YYYYMMDD\"" );
fprintf ( stderr, "%c\n", '\n' );
fprintf ( stderr, "%s\n", "Generates a SQL script for the month.
Input file is .\\SiteUpdate.sql" );
}
return rc;
}
int fgetline ( FILE *fp, char *buf )
{
char *p;
int c, i = 0;
p = buf;
while ( EOF != ( c = fgetc ( fp ) ) && '\n' != c && i < BUFSIZE )
{
*p = c;
++p;
++i;
}
*p = 0;
if ( ( p = strchr ( buf, '\n' ) ) ) *p = 0;
return c;
}
/* end SiteUpdate.c */