English 中文(简体)
保存/复制文件指针/描述符
原标题:Saving/duplicating a file pointer/descriptor

在有 < 强度 > 全球 < / 强度 > < code > FILE 指针/描述符的地方,我有一个要求。其中一项功能将从此指针/描述符中读取。 < 强度% em > 内部指针 与 < code > FILE 指针/ 描述符进步相关联的 < fILE < /code > 指针/描述符/描述符。此函数返回后, < 强度@ em > 我要从同一个 < code > FILE > < /code > 指针/描述符中读取, 以及其它函数所读的相同数据。

但我无法读取与内部指示器前进相同的精确数据 。 迭代描述符不工作 ,因为重复的反照原始。在阅读前保存 FILE 指针也无效,因为它是一个指针,并且将再次开始引用相同的东西 。

一种替代办法是在阅读前使用 fgetpos () 保存文件位置,然后在下次阅读前使用 fsetpos () () 。

但这对文件指针而不是描述符有效。

通常的指针很容易。 但用 < code> FILE 指针, 事情就变得困难了 。

是否有其他的unt-lumsy 方法可以做到这一点?

问题回答

如果您指的是 POSIX 文件描述符, 听起来您正在寻找 lseek ()

off_t off;

off = lseek(fd, 0, SEEK_CUR); /* get current offset */
/* do some read(s) */
lseek(fd, off, SEEK_SET);

另一个选项是使用“http://linux.die.net/man/2/mmap” rel=“nofollow”\\ code>mmap () 来绘制文件的内存图。

这将允许您随机阅读文件中的任何位置, 而不必使用 lseek () fseek ()

mmap () at linux questions 上,您将会在mmap ()

基于您的描述, 我会将文件指针隐藏在另一个界面后面, 缓冲最后读取的数据块, 这样代码的不同区域就可以访问相同的数据, 而不必倒转流 。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...