用_popen打开文件的fseek [英] fseek on a file opened with _popen

查看:248
本文介绍了用_popen打开文件的fseek的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


最近我发现了一些问题。我有一些C代码

,它决定了

文件中可用的字节数。之后我在读取字节之前使用这些信息来将一个缓冲区的大小正确地放大。

确定
$ b $中可用的字节数b文件分5个步骤完成:


1.使用tell(aFile)获取当前位置。

2.使用fseek(aFile,0, SEEK_END)移动到最后。

3.使用tell(aFile)获取当前位置(这是文件的

大小,以字节为单位)。

4.我移动到第1步中使用fseek()获得的位置。

5.将当前位置从文件大小减去

得到可用的字节数。


这段代码肯定不是最优雅的解决方案,但

它是可移植的。该代码适用于

windows和linux下的普通文件。可移植性也是原因

为什么我使用tell()和fseek()而不是windows特定的

代码。


当我使用_popen打开文件时,我得到了不同的结果:

- 在linux下,步骤1的tell()返回-1,这意味着

该文件不可搜索。我可以认识到这种情况

并做出相应的反应(我不能预先对缓冲区进行malloc。

而是我malloc一个较小的缓冲区重新分配,直到

读取所有字节。

- 在Windows下,步骤1的tell()返回0

表示该文件是可搜索的,当前位于0位。

fseek()和ftell()的其他调用也成功,

表示可用字节数为0.

因此我的程序认为在使用_popen打开的文件中没有字节




使用_popen打开文件的信息是

我的程序中没有该地方可用。


现在我的问题:

是否有可能找到一个文件(可在

类型为FILE *的变量是用_popen打开的?


类似于:将FILE *转换为句柄并询问

函数关于文件类型。对我来说没问题

在#ifdef下插入Windows特定代码

预先感谢Thomas Mertes


Seed7主页: http://seed7.sourceforge.net

Seed7 - 可扩展的编程语言:用户定义的语句

和运算符,抽象数据类型,没有特殊的模板
语法,带接口的OO和多个调度,静态类型,

解释或编译,可移植,在linux / unix / windows下运行。

解决方案

th *********** @ gmx.at 写道:


你好


最近我发现了一些问题。我有一些C代码

,它决定了

文件中可用的字节数。



[snip]


该代码适用于

下的普通文件Windows和Linux。



[snip]


当我用_popen打开文件时,我得到了不同的结果:

- 在linux下,步骤1的tell()返回-1,这意味着

该文件不可搜索。我可以认识到这种情况

并做出相应的反应(我不能预先对缓冲区进行malloc。

而是我malloc一个较小的缓冲区重新分配,直到

读取所有字节)。



我希望你不要一次增长一个字节:-)


- 在Windows下,步骤1的tell()返回0

表示该文件是可搜索的,目前处于位置0.

其他调用fseek()和ftell( )也成功并且

表示可用字节数为0.

因此我的程序认为没有字节

可用于用_popen打开文件。


它是用_popen打开的文件的信息是

在我的程序中的那个地方不可用。



您能否考虑通过提供包装器

机制使其可用? (这可能是我最喜欢的方法,而不是寻找平台细节......)


现在我的问题:

是否可以找到一个文件(在FILE *类型的
变量中可用)是用_popen打开的?



由于_popen不是C标准的一部分,所以我们在这里考虑的并不是真正的b $ b。你可能会在Windows新闻组中做得更好



th *********** @ gmx.at 写道:


你好


最近我发现了一些问题。我有一些C代码

,它决定了

文件中可用的字节数。之后我在读取字节之前使用这些信息来将一个缓冲区的大小正确地放大。

确定
$ b $中可用的字节数b文件分5个步骤完成:


1.使用tell(aFile)获取当前位置。



不是你的男人ftell()而不是告诉()?

如果不是你最不可能丢失在这里这不是标准功能。


2.使用fseek(aFile,0,SEEK_END)移动到最后。

3.使用tell(aFile)获取当前位置(这是文件的

大小,以字节为单位)。

4.我移动到我得到的位置在步骤1中使用fseek()。

5.将当前位置从文件大小减去

获取可用字节数。


这段代码当然不是最优雅的解决方案,但

它是便携式的。该代码适用于

windows和linux下的普通文件。可移植性也是原因

为什么我使用tell()和fseek()而不是windows特定的

代码。


当我用_popen打开一个文件时,我得到了一个不同的结果:



标准C中没有函数_popen()(我认为)。在POSIX中有popen()(即没有前导下划线的


- 在linux下,步骤1的tell()返回-1表示

该文件不可搜索。我可以认识到这种情况

并做出相应的反应(我不能预先对缓冲区进行malloc。

而是我malloc一个较小的缓冲区重新分配,直到

读取所有字节。

- 在Windows下,步骤1的tell()返回0

表示该文件是可搜索的,当前位于0位。

fseek()和ftell()的其他调用也成功,

表示可用字节数为0.

因此我的程序认为在使用_popen打开的文件中没有字节




使用_popen打开文件的信息是

我的程序中没有该地方可用。


现在我的问题:

是否有可能找到一个文件(可在

类型为FILE *的变量是用_popen打开的?


类似于:将FILE *转换为句柄并询问

函数关于文件类型。对我来说没问题

在#ifdef



OT下插入Windows特定代码(我认为)但是int filno (FILE * stream);可能是你要点什么

寻找


再见,Jojo


在文章< ; fq ********** @ aioe.org>,

Mark Bluemel< ma ********** @ pobox.comwrote:


> (我不能预先对缓冲区进行malloc。
而是我malloc一个较小的缓冲区,重新分配,直到读取所有字节)。


>我希望你不要一次增长一个字节:-)


大多数malloc()实现都没有太大问题。


- Richard

-

:wq


Hello

Recently I discovered some problem. I have some C code
which determines how many bytes are available in a
file. Later I use this information to malloc a buffer of
the correct size before I read the bytes.
Determining the number of bytes available in a
file is done in 5 steps:

1. Use tell(aFile) to get the current position.
2. Use fseek(aFile, 0, SEEK_END) to move to the end.
3. Get the current position with tell(aFile) (this is the
size of the file in bytes).
4. I move to the position which I got in step 1 with fseek().
5. Subtract the current position from the file size to
get the number of bytes available.

This code is certainly not the most elegant solution but
it is portable. The code works for normal files under
windows and linux. The portability is also the reason
why I use tell() and fseek() instead of windows specific
code.

When I open a file with _popen I get a different result:
- Under linux the tell() of step 1 returns -1 which means
the file is not seekable. I can recognice this situation
and react accordingly (I cannot malloc the buffer beforehand.
Instead I malloc a smaller buffer which is realloced until
all bytes are read).
- Under windows the tell() of step 1 returns 0 which
means the file is seekable and is currently at position 0.
The other calls of fseek() and ftell() succeed also and
indicate that the number of available bytes is 0.
Therefore my program thinks that there are no bytes
available in the file opened with _popen.

The information that it is a file opened with _popen is
not available at that place in my program.

Now my question:
Is it possible to find out that a file (available in a
variable of type FILE * ) was opened with _popen?

Something like: Turn the FILE * into a handle and ask a
function about the file type. It is no problem for me to
insert windows specific code under an #ifdef

Thanks in advance Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.

解决方案

th***********@gmx.at wrote:

Hello

Recently I discovered some problem. I have some C code
which determines how many bytes are available in a
file.

[snip]

The code works for normal files under
windows and linux.

[snip]

When I open a file with _popen I get a different result:
- Under linux the tell() of step 1 returns -1 which means
the file is not seekable. I can recognice this situation
and react accordingly (I cannot malloc the buffer beforehand.
Instead I malloc a smaller buffer which is realloced until
all bytes are read).

I hope you don''t grow it a byte at a time :-)

- Under windows the tell() of step 1 returns 0 which
means the file is seekable and is currently at position 0.
The other calls of fseek() and ftell() succeed also and
indicate that the number of available bytes is 0.
Therefore my program thinks that there are no bytes
available in the file opened with _popen.

The information that it is a file opened with _popen is
not available at that place in my program.

Could you consider making it available, by providing a wrapper
mechanism? (That would probably be my favoured approach, rather
than looking for platform specifics...)

Now my question:
Is it possible to find out that a file (available in a
variable of type FILE * ) was opened with _popen?

As _popen is not part of the C standard, it''s not really
something we would consider here. You''d probably do better
asking in a Windows newsgroup.


th***********@gmx.at wrote:

Hello

Recently I discovered some problem. I have some C code
which determines how many bytes are available in a
file. Later I use this information to malloc a buffer of
the correct size before I read the bytes.
Determining the number of bytes available in a
file is done in 5 steps:

1. Use tell(aFile) to get the current position.

Don''t you man ftell() rather than tell()?
If not you''re most üprobably lost here as that won''t be a standard function.

2. Use fseek(aFile, 0, SEEK_END) to move to the end.
3. Get the current position with tell(aFile) (this is the
size of the file in bytes).
4. I move to the position which I got in step 1 with fseek().
5. Subtract the current position from the file size to
get the number of bytes available.

This code is certainly not the most elegant solution but
it is portable. The code works for normal files under
windows and linux. The portability is also the reason
why I use tell() and fseek() instead of windows specific
code.

When I open a file with _popen I get a different result:

no function _popen() in standard C (I think). In POSIX there''s popen() (i.e.
without the leading underscore)

- Under linux the tell() of step 1 returns -1 which means
the file is not seekable. I can recognice this situation
and react accordingly (I cannot malloc the buffer beforehand.
Instead I malloc a smaller buffer which is realloced until
all bytes are read).
- Under windows the tell() of step 1 returns 0 which
means the file is seekable and is currently at position 0.
The other calls of fseek() and ftell() succeed also and
indicate that the number of available bytes is 0.
Therefore my program thinks that there are no bytes
available in the file opened with _popen.

The information that it is a file opened with _popen is
not available at that place in my program.

Now my question:
Is it possible to find out that a file (available in a
variable of type FILE * ) was opened with _popen?

Something like: Turn the FILE * into a handle and ask a
function about the file type. It is no problem for me to
insert windows specific code under an #ifdef

OT here (I think) but "int filno(FILE *stream);" might be what you''re
looking for

Bye, Jojo


In article <fq**********@aioe.org>,
Mark Bluemel <ma**********@pobox.comwrote:

> (I cannot malloc the buffer beforehand.
Instead I malloc a smaller buffer which is realloced until
all bytes are read).

>I hope you don''t grow it a byte at a time :-)

That''s not much of a problem with most malloc() implementations.

-- Richard
--
:wq


这篇关于用_popen打开文件的fseek的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆