C 从文件中动态获取数据

C 从文件中动态获取数据,c,file-io,C,File Io,我有文本文件emails.txt..这是如何在文本文件中输入 Emails.txt abc@gmail.com sfs@yahoo.com 我必须从文件中获取数据,并从数据中随机选择2个条目 谁能给我推荐一下做这件事的技巧吗 谢谢如果你使用C++,而不只是C++,你可以使用如下代码: #include <iostream> #include <fstream> #include <time.h> #include <vector> usin

我有文本文件emails.txt..这是如何在文本文件中输入

Emails.txt


abc@gmail.com
sfs@yahoo.com
我必须从文件中获取数据,并从数据中随机选择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到找到的行数范围内生成两个随机数,然后在查找您感兴趣的行时再次读取文件,或者您可以这样做:

文件名: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文件。

这将起作用:

#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?你需要更多的细节,伙计+1:你的电子邮件地址是256,真是太酷了!