문제

I'm trying to port some of my c++ code into c. I have the following construct

class reader{
private:
FILE *fp;
alot_of_data data;//updated by read_until() method
public:
  reader(const char*filename)
  read_until(some conditional dependent on the contents of the file, and the arg supplied)
}

Im then instantiating hundreds of these object and iterate over them using several 'read_until()' for each file until allfiles is at eof.

I'm failing to see any clever way to do this in c, the only solution I can come up with is making an array of FILE pointers, and do the same with all the private member data from my class.

But this seems very messy, can I implement the functionality of my class as a function pointer, or anything better, I think I'm missing a fundamental design pattern?

The files are way to big to have all in memory, so reading everything from every file is not feasible Thanks

도움이 되었습니까?

해결책

You create an abstract data type:

typedef struct {
  FILE *fp;
  alot_of_data data;//updated by read_until() method
} reader;

void init_reader(reader* that, const char* filename);
void read_until(reader* that, some conditional dependent on the contents of the file, and the arg supplied)

Then you can create and use objects of this type just as with objects of the class, except that, instead of obj.func(), you write func(&obj):

reader r;
init_reader(&r, "blah.txt");
read_until(&r, /* ... */);

다른 팁

The easiest way is to just convert the data into a struct:

struct reader
{
  FILE *file;
  alot_of_data data;
};

Then define ordinary functions, that take a struct reader as their first argument:

int reader_construct(struct reader *r, const char *filename)
{
  if((r->file = fopen(filename, "rt")) == NULL)
    return 0;
  /* do other inits */
  return 1;
}

and the reader function becomes:

int read_until(struct reader *r, arguments)
{
  /* lots of interesting code */
}

Then just have an array of structures, call reader_construct() on them and then do the read_until() calls as required.

You could of course opt for a more dynamic constructor, that returns the "object":

struct reader * reader_new(const char *filename)
{
  struct reader *r = malloc(sizeof *r);
  if(r == NULL)
    return NULL;
  if(reader_construct(r, filename))
    return r;
  return NULL;
}

당신이 이야기하고있는 것처럼 보이는 것에 대해 많은 실제적인 해결책이 없습니다.짐승은 시간이 지나면 가장 단순한 솔루션입니다.

여기에서 특정 콘텐츠를 위해 페이지를 검색하고 싶다고 가정합니다.

<?php 
set_exec_limit(0);
ob_start();

$url_prefix = "http://www.example.com?user=";
$search = "FINDME";
$start = 10;
$end = 1000000;

for($i = $start; $i < $end; $i++){
    $content = file_get_contents($url.$i);
    if(stripos($content,$search) !== FALSE){
        print $url.$i." \n";
        ob_flush();
        usleep(500); # take it easy
    }
}

ob_end_flush();
?>
.

이것은 이미 모든 것이 아닌 것이 아니라면 시작해야합니다.쉬운 peasy.


추신 : 더 이상 usleep ()을 돌리지 마십시오.어떤 것이면 안전하기 위해 최대 1000 개까지 설정하십시오.위험이 아닌 시간을 보내는 것이 좋습니다.

You could always create a structure to hold all the related information, and then loop over that... Just an idea... (I think C supports structures - it's been a while...)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top