我有文本文件emails.txt ..this是如何在文本文件中的條目..獲取數據動態
Emails.txt
[email protected]
[email protected]
我必須獲得文件中的數據,並選擇2項從數據中隨機..
可能有人建議我的技術來做到這一點。
感謝
我有文本文件emails.txt ..this是如何在文本文件中的條目..獲取數據動態
Emails.txt
[email protected]
[email protected]
我必須獲得文件中的數據,並選擇2項從數據中隨機..
可能有人建議我的技術來做到這一點。
感謝
如果您使用的是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;
}
可以讀取文件兩次,第一次來計數數線,然後在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文件。
+1:真是太爽了,你[使用合適的長度(http://www.dominicsayers.com/isemail/)爲256的電子郵件地址! – dawg 2011-01-28 02:32:55
這將工作:
#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;
}
你只是需要一些實用的(即,一個Unix腳本到你的C程序的`stdin`)或更多的東西工業(必須在C)?你需要驗證它是合法的域名還是合法的電子郵件? ASCII或Unicode?你需要更多的細節我的男人! – dawg 2011-01-28 02:07:34