作者stefany ()
看板Perl
标题[问题] c code 转 perl
时间Mon Jan 11 06:00:00 2010
问题是如果有10个不同的random files 每个file有栏位是tag
程式的目的是将所有file的tag丢到memory以後sort
再将sort之後的tag以及对应的data印到output file
如果用c写的话
可以使用file pointer以及fread fwrite
但如果用perl的话
有类似file pointer的概念吗?
The C code:
#define tag 100
#define data 1k
FILE fptr[i] = fopen(file[i]);
FILE *outptr;
void modify_quicksort (int &, int &, int);
int a[] = malloc(sizeof(tag)*n);
int buffer[] = malloc(sizeof(data)*n);
int index = 0;
for (i = 0, i < n, i++)
fread( &a[i], sizeof(tag), fptr[i] );
while (n != index)
{
modify_quicksort (*(a+index), *(fptr+index), n-index);
//return sorted array a[] and fptr[]
fread (&buffer, sizeof(data), 1, fptr[index]);
//read data from file to temporary buffer
//fptr will point to the next line at the same time
fwrite(&buffer, sizeof(data), 1, outptr);
//write data from buffer to output file
if (fptr[index] != feof())
fread (&a[index], sizeof(tag), 1, fptr[index]);
else
{
fclose(fptr[index]);
index++;
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 24.6.47.62
※ 编辑: stefany 来自: 24.6.47.62 (01/11 06:01)
※ 编辑: stefany 来自: 24.6.47.62 (01/11 06:06)