Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System Engineer Needing Help with a piece of code

Status
Not open for further replies.

cteas

Technical User
Sep 26, 2005
1
US
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");
}

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


 
1. Use CODE tag for your snippets (see Process TGML link on the form).
2. Automatic array above is uninitialized (has garbage contents). So lines_abv() copy a garbage (by strcpy) then overlapped a stack then crashed. It's a very strange function (for example, it copies unitialized array elements (from 100, and a pointer to a row of 2D array is never null).
3. This snippet never works in TC, Pacific C etc. It's wrong. Redesign it.
4. VC compiles it as usually.
5. Avoid large array (>10K bytes) allocation on the stack (automatic). Better use malloc in that cases...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top