Чтение определенного количества строк из файла в C (scanf, fseek, fgets) - PullRequest
1 голос
/ 13 октября 2010

У меня есть мастер процессов, который порождает N дочерних процессов , которые взаимодействуют с родителем через неназванные каналы. Я должен быть в состоянии:

  • заставляет отца открыть файл и затем отправить каждому ребенку структуру, сообщающую, что он должен прочитать от min до max строки;
  • это произойдет одновременно, поэтому я не знаю:
  • 1-й, как разделить total_lines для N карт и
  • 2-й, как мне заставить каждого ребенка читать только те строки, которые он должен?

Моя проблема не касается О.С. понятия, только файловые операции: S

Возможно, fseek? Я не могу отобразить файл журнала (некоторые имеют более 1 ГБ).

Буду признателен за некоторые идеи. Заранее спасибо

РЕДАКТИРОВАТЬ: я пытаюсь заставить детей читать соответствующие строки без использования fseek и значения кусков, так что, может кто-нибудь сказать мне, если это действительно так? :

//somewhere in the parent process:

    FILE* logFile = fopen(filename, "r");
                while (fgets(line, 1024, logFile) != NULL) {
                    num_lines++;
                }
                rewind(logFile);
                int prev = 0;
                for (i = 0; i < maps_nr; i++) {
                    struct send_to_Map request;
                    request.fp = logFile;
                    request.lower = lowLimit;
                    request.upper = highLimit;

                    if (i == 0)
                        request.minLine = 0;
                    else
                        request.minLine = 1 + prev;
                    if(i!=maps_nr-1)
                        request.maxLine = (request.minLine + num_lines / maps_nr) - 1;
                    else
                       request.maxLine = (request.minLine + num_lines / maps_nr)+(num_lines%maps_nr);
                    prev = request.maxLine;

                }
                //write this structure to respective pipe


//child process:

while(1) {
      ...
      //reads the structure to pipe (and knows which lines to read)
      int n=0, counter=0;
      while (fgets(line, 1024, logFile) != NULL){
         if (n>=minLine and n<=maxLine)
              counter+= process(Line);//returns 1 if IP was found, in that line, between the low and high limit
         n++; 
      }
     //(...) 
}

Я не знаю, сработает ли это, я просто заставляю это работать! Даже так, можно ли превзойти один процесс, считывая весь файл и печатая общее количество ips, найденных в файле журнала?

Ответы [ 3 ]

1 голос
/ 13 октября 2010
/* get an array with line-startpositions (file-offsets) */
fpos_t readLineBegins(FILE *f,fpos_t **begins)
{
  fpos_t ch=0, mark=0, num=0;
  *begins = 0;
  do {
    if( ch=='\n' )
    {
       *begins = realloc( *begins, ++num * sizeof(fpos_t) );
      (*begins)[num-1] = mark;
        mark = ftell(f);
    }
  } while( (ch=fgetc(f))!=EOF );

  if( mark<ftell(f) )
  {
    *begins = realloc( *begins, ++num * sizeof(fpos_t) );
    (*begins)[num-1]=mark;
  }

  return num;
}

/* output linenumber beg...end */
void workLineBlocks(FILE *f,fpos_t *begins,fpos_t beg,fpos_t end)
{
  while( beg<=end )
  {
    int ch;
    fsetpos( f, &begins[beg] ); /* set linestart-position */
    printf("%ld:", ++beg );
    while( (ch=fgetc(f))!=EOF && ch!='\n' && ch!='\r' )
      putchar(ch);
    puts("");
  }
}

main()
{
  FILE *f=fopen("file.txt","rb");
  fpos_t *lineBegins, /* Array with line-startpositions */
  lb = readLineBegins(f,&lineBegins); /* get number of lines */

  workLineBlocks(f,lineBegins,lb-2,lb-1); /* out last two lines */
  workLineBlocks(f,lineBegins,0,1); /* out first two lines */

  fclose(f);
  free(lineBegins);
}
1 голос
/ 15 октября 2010
1 голос
/ 13 октября 2010

Если вас не волнует равномерное деление файла точно и распределение длин строк несколько равномерно по всему файлу, вы можете избежать чтения всего файла в родительском файле один раз.

  1. Получить размер файла.
  2. chunk_size = file_size / number_of_children
  3. Когда вы порождаете каждого ребенка, делайте в родительском:
    • искать (child_num + 1) * chunk_size
    • Читайте вперед, пока не найдете новую строку.
    • порождает дочернего элемента, сообщая ему, что он должен начинаться в конце предыдущего фрагмента (или 0 для первого дочернего элемента), и фактической длины фрагмента.
  4. Каждый ребенок ищет start и читает chunk_size байт.

Это грубый набросок стратегии.

Отредактировано для упрощения.

Редактировать : вот некоторый непроверенный код для шага 3 и шага 4 ниже. Это все не проверено, и я не очень внимательно относился к ошибкам, но это дает вам представление об использовании fseek и ftell, что звучит как то, что вы ищете.

// Assume FILE* f is open to the file, chunk_size is the average expected size,
// child_num is the id of the current child, spawn_child() is a function that
// handles the logic of spawning a child and telling it where to start reading,
// and how much to read. child_chunks[] is an array of structs to keep track of
// where the chunks start and how big they are.
if(fseek(f, child_num * chunk_size, SEEK_SET) < 0) { handle_error(); }
int ch;
while((ch = fgetc(f)) != FEOF && ch != '\n')
{/*empty*/}

// FIXME: needs to handle EOF properly.
child_chunks[child_num].end = ftell(f); // FIXME: needs error check.
child_chunks[child_num+1].start = child_chunks[child_num].end + 1;
spawn_child(child_num);

Затем в вашем ребенке (шаг 4) предположим, что ребенок имеет доступ к child_chunks[] и знает его child_num:

void this_is_the_child(int child_num)
{
    /* ... */

    fseek(f, child_chunks[child_num].start, SEEK_SET); // FIXME: handle error
    while(fgets(...) && ftell(f) < child_chunks[child_num].end)
    {
    }
}
...