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!

Urgently in need of C code for. ...read inside??

Status
Not open for further replies.

perlguy

Programmer
May 17, 2001
26
US
I would like to write a C prg. which should do exactly what my Perl prg. does. Here is what i need. There is a file called "list.txt" which has following records:
1=bill gates=MicroSoft
2=Sam walton=Walmart
3=Steve Jobs=Apple
4=Larry w=Oracle


Now prg. should generate/print a new record each time it gets executed. To be more clear it 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. For example If it generates random no. 2 , it should read the file and print details of record that corr. to 2 which is in this case is : 2=Sam walton=Walmart

Any help . Greatly appreciated.
regards
sk
 
Here is a short outline of what you
could do:

1. Generate a random number between 0 & 5 using srand()
and rand();
2. If your random number is 'n', read n-1 lines from the
file using fopen (to open) and fscanf() or fgets()
to read the lines.
3. Read the next line from the file and print it out.

Hope this helps.

abp :cool:

 
This program may fulfil your need.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
FILE * fp;
char cdata[100];
int number, rnd_number;
time_t t;
srand( (unsigned) time(&t));
while( (rnd_number = rand() % 4) == 0);
printf(&quot;\n.......%d........&quot;, rnd_number);
fp = fopen(&quot;list.txt&quot;, &quot;r&quot;);
fscanf(fp, &quot;%d&quot;, &number);
while( !feof(fp))
{
fgets(cdata, 100, fp);
if(number == rnd_number)
{
printf(&quot;\n\n\n........Found......\n\n\n&quot;);
printf(&quot;\n %d %s&quot;, number, cdata);
fclose(fp);
exit(0);
}
fscanf(fp, &quot;%d&quot;, &number);
}
printf(&quot;\n\n\n .......Not Found........\n&quot;);
fclose(fp);
return 0;
}

Regards
Maniraja S
 
Thanks Smaniraja and abp. Your logic works great. I will be adding some more logic to it. If i need more help , I will definately post here.

Appreciated.
 
need one more favour:
while((rnd_number = rand() % 4) == 0);
Instead of using 4 as a fixed in the above line what if I store it in some &quot;totalrec.txt&quot; , that way in future if I add further records to &quot;list.txt&quot; then I will just change the total records in &quot;totalrec.txt&quot; file. This way I will not have to compile my C prg. everytime I add records. How do I do this??
Another thing I would like to split the record data, There is a split() in in perl which allows you to split records using a modifier eg.(in Perl I do as below:)
($var1,$var2,$var3) = split(/=/,$rec);
This way if my record is let say
1=Bill gates=Microsoft
the it will store following values :
$var1 = 1
$var2 = Bill Gates
$var3 = Microsoft

PS. '=' sign is record data seperator.

Appreciated. regards.
perlguy.
 
perlguy,
instead of keeping number of records in an another file ,you can count number of records from your file itself
by adding a record count function

int recordcount()
{
int recordcnt=0;
while( !feof(fp))
{
fgets(cdata, 100, fp);
recordcnt += 1;

}
return recordcnt;
}
Just check the scope of your variables like (FILE *fp , char *cdata).
You need to write your own split function where you will call it with string (which need to be seprated) along with the delimiter and it will return you the seprated variables
some thing like

char *splitstring(char *string_to_be_splitted , char delimiter , int delimiter_value )

read string char by char till you get delimiter, increase delimiter count by 1 ,if it matches with your delimiter value-1 ,read character by character after that in a string till you get another delimiter.

}
 
to split the data ,couldnt we use a structure to store data into the file and a structure to retreive data,then we could have &quot;structure.name&quot; as &quot;bill gates&quot;

e.g of struct----
struct names
{
int num;
char name[20];
char compname[20];
};
struct names a;

the record will be of 42 bytes so to get the nth record we can position the file pointer to 42*n bytes and from there read the next 42 variables in the structure and then use &quot;a.num&quot;, &quot;a.name&quot; ---

the functions fseek() for jumping to the record
fscanf() for reading from the file will be needed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top