C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-06-2009, 09:19 AM   #1
Registered User
 
Join Date: Jul 2009
Posts: 2
little program to get time from log

few weeks ago i had a problem. there was a log with some data for about 150-200 lines.
and i was need to found a digits at every line and calculate a sum of them. it was not easy to type 150 values and 150 '+' manually with a calc so i tried to found some redactor or tool to do it but didn't find anything. so i wrote some little program to do it.
For example, part of log ( i was need these ms) :

[demod/stv362/s362lock.c:189][0] s362_Lock() Status[2] Time[500ms]
[demod/stv362/s362lock.c:189][0] s362_Lock() Status[2] Time[400ms]
[demod/stv362/s362lock.c:189][0] s362_Lock() Status[2] Time[1800ms]
[demod/stv362/s362lock.c:189][0] s362_Lock() Status[2] Time[500ms]

Now I just want to show the code and listen some suggestions about it. For me it works fine but still linked list are not supported (for files with unknown number of lines), and 2 'Time' for one line not supported too. and there is no check for unsigned int variable overflow. If you find something else - you are welcome.
P.S. This is just for fun.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char *version="0.53";
#define CHARS_PER_STRING 300
//#define DEBUG_ENABLE


FILE * file_ptr;
int times[300];  // strings in file

int FileScan(FILE* file_ptr, const char * sequence)
{
	unsigned int i;
	unsigned int sequence_size=0;
	unsigned int total=0;
	unsigned char have_sequence=0;
	char tmp_string[CHARS_PER_STRING];   //array to contain one string
	char * tmp_str_ptr;
  char digit[10];
  int digit_cnt=0;
	unsigned int strings_seq_cnt=0;     //strings with sequence found
	unsigned int strings_total_cnt=0;   //total number of strings in file

	sequence_size=strlen(sequence);
  
#ifdef DEBUG_ENABLE
	printf ("sequence: %s \n", sequence);
	printf("size of seq %d\n", sequence_size);
#endif

	//working till the EOF, one iteration == one string have read
	while ( fgets(tmp_string,CHARS_PER_STRING,file_ptr) != NULL )
	{
		tmp_str_ptr=tmp_string;
		have_sequence=0;
  		while (*tmp_str_ptr)
  		{
    			if (*tmp_str_ptr == *sequence)
    			{
      				if ( !strncmp(tmp_str_ptr,sequence,sequence_size) ) //if we found Time[
      				{
#ifdef DEBUG_ENABLE
					printf("seq found\n");
#endif
		        		tmp_str_ptr += sequence_size ;   //set pointer to time digits
        				while (*tmp_str_ptr >= '0' && *tmp_str_ptr <= '9')
        				{
	  					if (digit_cnt >= 10)
	  					{
	  						printf("[error] value found is too big. \n");
							return 1;
	  					}
          					digit[digit_cnt]=*tmp_str_ptr;
          					tmp_str_ptr++;
          					digit_cnt++;
	  					have_sequence=1;
        				}
#ifdef DEBUG_ENABLE
					printf ("digit %d \n",digit_cnt);
#endif
      				}
    			}
    			tmp_str_ptr++;
  		}

		if (have_sequence)
		{
			times[strings_seq_cnt]=atoi(digit);
			strings_seq_cnt++;
		}
  		for (i=0; i<10; i++)
    			digit[i]='a';
  		strings_total_cnt++;  //increased for every cycle iteration, contains number of strings in file
  		digit_cnt=0;

	}

	for (i=0; i<strings_seq_cnt; i++)
 	{
   		total+=times[i];
   		printf ("string: %d time : %d \n",i+1,times[i]);
 	}
 	printf ("strings total:         %d \n",strings_total_cnt);
	printf ("strings with sequence: %d \n",strings_seq_cnt);
	printf ("time sum:              %d \n",total);
 	if (strings_seq_cnt)
		total/=strings_seq_cnt;
	printf ("time~: %d\n",total);

	return 0;
}


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

#ifdef DEBUG_ENABLE
	printf("timefromlog started. version: %s\n",version);
	printf("argc=%d\n",argc);
	for (i=0; i<argc; i++)
	{
		printf("argv[%d]: %s\n",i,argv[i]);
	}

#endif
	//parsing input without getopt() ! 
	if (argc == 2)
	{
		if ( !(strcmp(argv[1],"--help") ) )  
			printf ("this is a help for timefromlog program.\nfirst param: filename to search in.\nsecond param: sequence digits start after.\n --help    - this help\n --version - program version\n");
		else if ( !(strcmp(argv[1],"--version") ) )  
			printf ("version: %s  \n", version);
		else
			printf("incorrect params. run program with --help\n");
		return 1;
	}       
	else if (argc != 3)
	{
		printf("incorrect params. run program with --help\n");
		return 1;
	}

	file_ptr=fopen(argv[1],"r");
	if (!file_ptr)
	{
		printf("[error] : can't open file\n");
		return 1;
	}
	else 
		printf("[working] : file opened\n");

	FileScan(file_ptr, argv[2]);
	
	if ( !fclose(file_ptr) )
		printf("[working] : file closed successfully\n");
	else 
		printf("[error] : file can not be closed\n");
  

  return 0;
}
repu1sion is offline   Reply With Quote
Old 07-06-2009, 09:37 AM   #2
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
As the OS is a Unix variant it might be better to write a perl or awk script instead of trying to code the whole thing. Just my 2c though.
itCbitC is offline   Reply With Quote
Old 07-06-2009, 09:44 AM   #3
Registered User
 
Join Date: Jul 2009
Posts: 2
for me its more simple to write C code instead of perl script
repu1sion is offline   Reply With Quote
Old 07-06-2009, 10:04 AM   #4
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
Only because you haven't invested any time in learning about text processing scripting languages.

Compare
Code:
my $total = 0;
while ( <> ) {
  $total += $1 if ( /Time[(\d+)/ )
}
print "Total is $total\n";
That replaces 30 to 40 lines of your C.

Yes, it will take longer to write any given perl program compared to writing it in C. But if you never make a start, you're stuck. But once you're over the hump as it were, then you're knocking out simple perl programs in minutes which would take days to write in C.


Now onto your code.
Your digit array has no conspicuous \0 at the end of it, nor would there be room for one with this condition.
if (digit_cnt >= 10)

The first time around, the array is uninitialised, so you're hoping atoi() will do the right thing at the first junk character.


> printf ("this is a help f....
Try folding the lines, like so
Code:
printf ("this is a help for timefromlog program.\n"
         "first param: filename to search in.\n"
         "second param: sequence digits start after.\n"
         " --help    - this help\n"
         " --version - program version\n");
> if ( !strncmp(tmp_str_ptr,sequence,sequence_size) )
For better readability, try
if ( strncmp(tmp_str_ptr,sequence,sequence_size) == 0 )
It means the same thing, and it is as documented in the manual page.
A single ! character can easily be missed by simple scanning of the code.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 07-06-2009, 10:05 AM   #5
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
Never mind my post since I missed the "P.S. This is just for fun." disclaimer.
itCbitC is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Local Time Program Ronzel C++ Programming 1 06-18-2009 07:19 AM
How to time how long a program takes to run? advancedk C Programming 2 08-18-2008 07:50 PM
auto log in program .... twomers C++ Programming 15 01-16-2006 06:39 AM
Computer Startup Log Program Junior89 C++ Programming 8 01-11-2005 11:00 PM
write a program that will set the time to 5:58 jupimako C++ Programming 2 11-05-2003 03:51 AM


All times are GMT -6. The time now is 05:06 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22