I have text file emails.txt..this is how the entry in the text file..
Emails.txt
abc@gmail.com
sfs@yahoo.com
i have to obtain the data from 开发者_如何学运维file and select 2 entries from the data randomly..
could anyone suggest me the technique to do this.
Thanks
You can read the file twice, first time to count the number of lines, then generate two random numbers in the range of 0 to number_of_lines found and then read the file again while looking for the lines you are interested in or you can do it like this:
Filename: emails.c #include
int main (int argc, char **argv)
{
// open a handler to your file (read)
FILE *fp = fopen("emails.txt", "r");
// check if we have successfully opened the file for reading
if (fp != NULL)
{
// in your case 256 characters is enough for line size
// since emails are not that long but if longer buffer overflow
// is very possible and its not helpful as stackoverflow.com is :p
char line_buffer[256];
// count the number of lines read
unsigned int lines_read = 0;
// read up to line size or until EOL (End of Line) or EOF (End of File)
// will return NULL on error or eof
while (fgets(line_buffer, sizeof(line_buffer), fp) != NULL) {
// use rand() and seed it with the number of lines read
if ((rand() % ++lines_read) == 0) {
// do something with this line, it was randomly picked
// for the example, will print it on the screen
printf("%s \n", line_buffer);
}
}
// close file handler as we don't need it anymore
fclose(fp);
}
// return to the OS
return 0;
}
NOTE: This is C implementation, so save as .c file.
If you are using c++, and not just c -- you can use something like the following code:
#include <iostream>
#include <fstream>
#include <time.h>
#include <vector>
using namespace std;
int getrand(int num, int notnum)
{
int result = 0;
while (true)
{
result = abs(rand()) % num + 1;
if (result != notnum)
{
return result;
}
}
}
int main()
{
ifstream emails;
srand(time(NULL));
emails.open("emails.txt");
string email;
vector<string> emailVector;
while (emails >> email)
{
emailVector.push_back(email);
}
int index1 = getrand(emailVector.size(), 0);
int index2 = getrand(emailVector.size(), index1);
cout << "email 1: " << emailVector[index1 - 1] << endl;
cout << "email 2: " << emailVector[index2 - 1] << endl;
}
This will work:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BUF_SIZE 4096
#define RAND_COUNT 2
int count_lines(FILE *fp) {
char buf[BUF_SIZE];
int line_count=0;
fseek(fp, 0L, SEEK_SET);
while(fgets(buf, BUF_SIZE, fp) != NULL) {
line_count++;
}
fseek(fp, 0L, SEEK_SET);
return line_count;
}
int line_num(FILE *fp, char *buf, int line_num){
fseek(fp, 0L, SEEK_SET);
int i=0;
while(fgets(buf, BUF_SIZE, fp) != NULL) {
if (++i == line_num) {
return i;
}
}
return -1;
}
int main (int argc, const char * argv[]) {
FILE *fp=NULL;
char buf[BUF_SIZE];
char name[]="email.txt";
if((fp=fopen(name, "r"))==NULL){
printf("can't open: %s\n\n",name);
return -1;
}
int line_count=count_lines(fp);
printf("line count=%i\n",line_count);
srand ((unsigned int)time(NULL));
for (int i=1; i<=line_count; i++) {
line_num(fp,buf,i);
printf("%i = %s",i,buf);
}
for (int i=0; i<RAND_COUNT; i++) {
int a=(rand() % line_count);
line_num(fp,buf,a);
printf("line %i = %s\n",a,buf);
}
fclose(fp);
return 0;
}
精彩评论