2011-01-28 65 views
-1

我有文本文件emails.txt ..this是如何在文本文件中的条目..获取数据动态

Emails.txt 


[email protected] 
[email protected] 

我必须获得文件中的数据,并选择2项从数据中随机..

可能有人建议我的技术来做到这一点。

感谢

+0

你只是需要一些实用的(即,一个Unix脚本到你的C程序的`stdin`)或更多的东西工业(必须在C)?你需要验证它是合法的域名还是合法的电子邮件? ASCII或Unicode?你需要更多的细节我的男人! – dawg 2011-01-28 02:07:34

回答

0

如果您使用的是C++,并且不仅仅是C - 您可以使用类似下面的代码:

#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; 
} 
1

可以读取文件两次,第一次来计数数线,然后在0至发现NUMBER_OF_LINES的范围内产生两个随机数,然后再读取该文件,同时寻找你感兴趣的线路,也可以做这样的:

文件名:emails.c #包括

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; 
} 

注意:这是C实现,所以保存为.c文件。

+0

+1:真是太爽了,你[使用合适的长度(http://www.dominicsayers.com/isemail/)为256的电子邮件地址! – dawg 2011-01-28 02:32:55

0

这将工作:

#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; 
}