Hi everyone.
Sorry to start this new thread , but had no choice since earlier thread was going too lengthy. With the help of some good experts in this forum I have come up with following solution for my following need:
File "list.txt" consist records as :
1=bill gates=MicroSoft
2=Sam walton=Walmart
3=Steve Jobs=Apple
4=Larry Ellison=Oracle
Now prg. should generate random number between 1 to 4 (since i am giving eg. of 4 records)and should print record details corr. to that number and will write the output in another file called 'list.out'. For example If it generates random no. 2 , it should read the file and print details of record that corr. to 2 in the following fashion:
Sam walton
Walmart
Then it should write above details in 'list.out' as below:
Name=Sam Walton=Company=Walmart
## SOLUTION: (very close but not 100% to the point)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
FILE *fp;
char *pch, cdata[200];
char recflag = 'N';
int number, rnd_number;
time_t t;
/* for random number */
srand( (unsigned) time(&t));
while((rnd_number = rand() % 5) == 0);
printf("RANDOM = %d \n", rnd_number);
fp = fopen("list.txt", "r"
fscanf(fp, "%d", &number);
while( !feof(fp))
fgets(cdata, 100, fp);
if(number == rnd_number)
{
printf("RECNO :: %d \n", number);
printf("DATA :: %s \n", cdata);
recflag = 'Y';
break;
}
fscanf(fp, "%d", &number);
}
fclose(fp);
if(recflag == 'N') printf("\n Not Found !!"
if(recflag == 'Y')
{
/* perform splitting */
pch = strtok(cdata, "="
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, "="
}
}
/* Don't know how to store above values in
other varibales so that I can write in 'list.out' */
/* can anyone do the rest ...... */
return 0;
}
The above prg. works fine and prints details the way i want,but the only problem is I want store these values in another tmp variables so that I can write it in 'list.out' file.
Please shed some thoughts on this. Can anyone come up with some good code for the above. Both logic wise and performance wise. I wnat this code to be very efficient and faster also error proof.
regards
perlguy.
Sorry to start this new thread , but had no choice since earlier thread was going too lengthy. With the help of some good experts in this forum I have come up with following solution for my following need:
File "list.txt" consist records as :
1=bill gates=MicroSoft
2=Sam walton=Walmart
3=Steve Jobs=Apple
4=Larry Ellison=Oracle
Now prg. should generate random number between 1 to 4 (since i am giving eg. of 4 records)and should print record details corr. to that number and will write the output in another file called 'list.out'. For example If it generates random no. 2 , it should read the file and print details of record that corr. to 2 in the following fashion:
Sam walton
Walmart
Then it should write above details in 'list.out' as below:
Name=Sam Walton=Company=Walmart
## SOLUTION: (very close but not 100% to the point)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
FILE *fp;
char *pch, cdata[200];
char recflag = 'N';
int number, rnd_number;
time_t t;
/* for random number */
srand( (unsigned) time(&t));
while((rnd_number = rand() % 5) == 0);
printf("RANDOM = %d \n", rnd_number);
fp = fopen("list.txt", "r"
fscanf(fp, "%d", &number);
while( !feof(fp))
fgets(cdata, 100, fp);
if(number == rnd_number)
{
printf("RECNO :: %d \n", number);
printf("DATA :: %s \n", cdata);
recflag = 'Y';
break;
}
fscanf(fp, "%d", &number);
}
fclose(fp);
if(recflag == 'N') printf("\n Not Found !!"
if(recflag == 'Y')
{
/* perform splitting */
pch = strtok(cdata, "="
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, "="
}
}
/* Don't know how to store above values in
other varibales so that I can write in 'list.out' */
/* can anyone do the rest ...... */
return 0;
}
The above prg. works fine and prints details the way i want,but the only problem is I want store these values in another tmp variables so that I can write it in 'list.out' file.
Please shed some thoughts on this. Can anyone come up with some good code for the above. Both logic wise and performance wise. I wnat this code to be very efficient and faster also error proof.
regards
perlguy.