I have wrote the following C prg. to display random record contents from text file 'pix.db'. 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);
}
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);
}