动态获取文件中的数据

时间:2011-01-28 01:50:40

标签: c file-io

我有文本文件 emails.txt ..这就是文本文件中的条目..

Emails.txt


abc@gmail.com
sfs@yahoo.com

我必须从文件中获取数据并随机从数据中选择2个条目。

任何人都可以建议我这样做的技巧。

由于

3 个答案:

答案 0 :(得分:1)

你可以读取文件两次,第一次计算行数,然后在0到number_of_lines的范围内生成两个随机数,然后在找到你感兴趣的行时再次读取文件,或者你可以这样做:

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

注意:这是C实现,因此另存为.c文件。

答案 1 :(得分: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;
}

答案 2 :(得分: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;
}