Hi All,
This is my first time using this forum. It's been 5 years since I've had to write any code. 7 years for C. I found some of the code below on the internet. The rest I screwed up myself. It sort of works on Turbo C. It works better on Pacific C, and it doesn't work at all VS 6. If anyone can help please give me some pointers. I think it generates a memory prob. This code is supposed to find a string in a text file, and then print the number of lines above that is passed by command line arguments.
/*****************************************************************
Program: trgrep
File: trgrep.c
Functions: Main, Search, Display_Usage
Description: Searches a text file for a user-specified string
Reports the line numbers where the target is found
Author: Richard Kagaba Barungi
Ammended: Charles Teasley
Environment: Turbo C, version 2.1, 1.8ghz 512mb RAM, Windows 2000 Pro
Revisions: 1.01a 09/22/05 (Added above and below implementation)
*********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define err_mac(x) printf("Program %s: (%d) Error opening file", __FILE__, __LINE__);
#define MAX_STR 256
#define A_LENG 100
int searchFile(char *str, char *fname, int ab);
void display_usage(char *filename);
void lines_abv(char above_ptr[][MAX_STR]);
main(int argc, char *argv[])
{
int result;
/* illegal number of parameters */
while ( argc < 4 || argc > 4 )
{
display_usage(argv[0]);
exit(1);
}
system("cls");
result = searchFile(argv[2], argv[1], atoi(argv[3]));
if (result == -1)
{
perror("Error conditioned encountered!");
printf("\aerrno = %d.\n", errno);
err_mac(" ");
puts("\nNow exiting program....");
exit(1);
}
return 0;
}
/*--------------------------------------------------------------*
* Function: SearchFile() *
*--------------------------------------------------------------*/
int searchFile(char *str, char *fname, int ab)
{
FILE *fp;
int line = 1, find = 0, place_holder;
char temp[MAX_STR];
char above[A_LENG][MAX_STR];
system("cls");
if ( (fp = fopen(fname, "r")) == NULL)
return(-1);
place_holder=ab;
while ( fgets(temp, 256, fp) != NULL)
{
strcpy(above[0], temp);
lines_abv(above);
if ( (strstr(temp, str)) != NULL)
{
while (ab > 0){
printf("%s",above[ab]);
ab=ab-1;
}
printf("***FOUND Line No.%d*** ", line);
printf("%s", temp);
printf("\n");
find++;
}
line++;
ab = place_holder;
}
if (find == 0)
printf("\n\t\t\aSorry match no found.");
return 0; /* compatability with later C++ compilers */
}
/*---------------------------------------------------------------*
* Function: void lines_abv(char *abv[][]) *
*---------------------------------------------------------------*/
void lines_abv(char above_ptr[][MAX_STR])
{
int i=100;
while (i > 0){
if (above_ptr != NULL){
strcpy(above_ptr, above_ptr[i-1]);
i--;}
}
return;
}
/*---------------------------------------------------------------*
* Function: display_usage() *
*---------------------------------------------------------------*/
void display_usage( char *filename )
{
system("cls");
printf("\nTRGREP.EXE. [Version 0.90] Charles Teasley 2005.");
printf("\n\nUSAGE: %s <file> <string> <lines above>", filename );
printf("\n\n where <file> is text file from which data obtained");
printf("\n and <string> is string to be located.\n\n");
}
/*-----------------------------------------------------------------*/
This is my first time using this forum. It's been 5 years since I've had to write any code. 7 years for C. I found some of the code below on the internet. The rest I screwed up myself. It sort of works on Turbo C. It works better on Pacific C, and it doesn't work at all VS 6. If anyone can help please give me some pointers. I think it generates a memory prob. This code is supposed to find a string in a text file, and then print the number of lines above that is passed by command line arguments.
/*****************************************************************
Program: trgrep
File: trgrep.c
Functions: Main, Search, Display_Usage
Description: Searches a text file for a user-specified string
Reports the line numbers where the target is found
Author: Richard Kagaba Barungi
Ammended: Charles Teasley
Environment: Turbo C, version 2.1, 1.8ghz 512mb RAM, Windows 2000 Pro
Revisions: 1.01a 09/22/05 (Added above and below implementation)
*********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define err_mac(x) printf("Program %s: (%d) Error opening file", __FILE__, __LINE__);
#define MAX_STR 256
#define A_LENG 100
int searchFile(char *str, char *fname, int ab);
void display_usage(char *filename);
void lines_abv(char above_ptr[][MAX_STR]);
main(int argc, char *argv[])
{
int result;
/* illegal number of parameters */
while ( argc < 4 || argc > 4 )
{
display_usage(argv[0]);
exit(1);
}
system("cls");
result = searchFile(argv[2], argv[1], atoi(argv[3]));
if (result == -1)
{
perror("Error conditioned encountered!");
printf("\aerrno = %d.\n", errno);
err_mac(" ");
puts("\nNow exiting program....");
exit(1);
}
return 0;
}
/*--------------------------------------------------------------*
* Function: SearchFile() *
*--------------------------------------------------------------*/
int searchFile(char *str, char *fname, int ab)
{
FILE *fp;
int line = 1, find = 0, place_holder;
char temp[MAX_STR];
char above[A_LENG][MAX_STR];
system("cls");
if ( (fp = fopen(fname, "r")) == NULL)
return(-1);
place_holder=ab;
while ( fgets(temp, 256, fp) != NULL)
{
strcpy(above[0], temp);
lines_abv(above);
if ( (strstr(temp, str)) != NULL)
{
while (ab > 0){
printf("%s",above[ab]);
ab=ab-1;
}
printf("***FOUND Line No.%d*** ", line);
printf("%s", temp);
printf("\n");
find++;
}
line++;
ab = place_holder;
}
if (find == 0)
printf("\n\t\t\aSorry match no found.");
return 0; /* compatability with later C++ compilers */
}
/*---------------------------------------------------------------*
* Function: void lines_abv(char *abv[][]) *
*---------------------------------------------------------------*/
void lines_abv(char above_ptr[][MAX_STR])
{
int i=100;
while (i > 0){
if (above_ptr != NULL){
strcpy(above_ptr, above_ptr[i-1]);
i--;}
}
return;
}
/*---------------------------------------------------------------*
* Function: display_usage() *
*---------------------------------------------------------------*/
void display_usage( char *filename )
{
system("cls");
printf("\nTRGREP.EXE. [Version 0.90] Charles Teasley 2005.");
printf("\n\nUSAGE: %s <file> <string> <lines above>", filename );
printf("\n\n where <file> is text file from which data obtained");
printf("\n and <string> is string to be located.\n\n");
}
/*-----------------------------------------------------------------*/