Any better & efficient alternative to this C prg.
I have wrote the following C prg. to display random record contents from text file 'pix.db' (free format). It generates random num from 1 to 10 and then fetches corr. record from 'pix.db'
File pix.db format: (note '|' is my field sepator)
ID|pix file|pic name
for example:
1|pix1.gif|pix1name
2|pix2.gif|pix2name
3|pix3.gif|pix3name
;;
;;
;;
10|pix10.gif|pix10name
Here is the 'C' code which I consider best but I don't know how efficient it is..... Does anyone know better & efficeint code for the following code....I am not 'C' guru....but I know this forum has some finest 'C' experts....hope they help me out here....Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void file_process(char *flname, int rnd_num, char *cdata);
void err_default(void);
int main(void)
{
FILE *rd_fp;
char *temp, *pch, *pname, *pix, got_err='N';
int pnum = 10; /* for random no. any record between 1..10 */
temp = (char *) malloc( sizeof(char) * 150);
pch = (char *) malloc( sizeof(char) * 150);
/* calling function passing 'pnum' */
file_process("pix.db", pnum, temp);
/* split the record */
strcpy(pch, strtok(temp, "|"));
if ( (pix = strdup(pch)) == NULL) got_err='Y';
strcpy(pch, strtok (NULL, "|"));
if ( (pname = strdup(pch)) == NULL) got_err='Y';
if(got_err == 'Y') print_error(); /* if error show default pix */
/* display content */
printf ("%s = %s", pix, pname);
free(temp); free(pch);
free(pix); free(pname);
return(0);
}
void file_process(char *flname, int rnd_num, char *cdata)
{
FILE *fp;
char recflag = 'N';
int rec_number, rnd_number;
time_t t;
/* generate random number */
srand((unsigned) time(&t));
rnd_number = rand() % rnd_num+1;
/* now get the corr. record */
if ((fp = fopen(flname, "r")) == NULL) print_error();
fscanf(fp, "%d", &rec_number);
while( !feof(fp))
{
fgets(cdata, 150, fp);
if(rec_number == rnd_number)
{
recflag = 'Y';
break;
}
fscanf(fp, "%d", &rec_number);
}
fclose(fp);
if(recflag == 'N') err_default();
}
void print_error(void)
{
printf ("ERROR - NO REC FOUND \n");
exit(1);
}
Couple of concern red your code?
Hi Salem,
Thanks for the alternative code ......looks clean and shorter than mine. BUT I have couple of concerns regd. the code they are:
1) char buff[BUFSIZ]
what is BUFSIZ and its size, don't you need to define it somewhere.
2) int n;
why did you defined it inside for loop......any specific reason.
Appreciated. Once again thanks a million.
perlguy.
Re: Couple of concern red your code?
Quote:
1) char buff[BUFSIZ]
what is BUFSIZ and its size, don't you need to define it somewhere.
Yep, it's defined in stdio.h, so don't worry.
Quote:
2) int n;
why did you defined it inside for loop......any specific reason.
That way the variable is only visible within the loop, and not within the rest of the function. This is the same for most variables created within braces.
Re: One last question....
Quote:
1) should I free memory used by *filename, *filedesc, *p variables or not? or is it done by system automatically when prg. exits...
2) Is there any software to test performance of this prg.?
for example: In perl/cgi we have some test tools that tells how many request perl/cgi prg. can handle at a time......
1 - No you don't. The memory is owned by the buff variable.
2 - Dunno! But you could write your own timer functions to encapsulate your code.