- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在使用 fseek 时遇到了问题。我有一个包含获取的 HTTP 数据的文件指针。然后我让 libmagic 确定文件的 mime 类型,然后想倒带:
char *mime_type (int fd)
{
char *mime;
magic_t magic;
magic = magic_open(MAGIC_MIME_TYPE);
magic_load(magic, MAGIC_FILE_NAME);
mime = (char*)magic_descriptor(magic, fd);
magic_close(magic);
return (mime);
}
int fetch_pull() {
fetch_state = fopen("/tmp/curl_0", "r");
if (fetch_state == NULL) {
perror("fetch_pull(): Could not open file handle");
return (1);
}
fd = fileno(fetch_state);
mime = mime_type(fd);
if (fseek(fetch_state, 0L, SEEK_SET) != 0) {
perror("fetch_pull(): Could not rewind file handle");
return (1);
}
if (mime != NULL && strstr(mime, "text/") != NULL) {
/* do things */
} else if (mime != NULL && strstr(mime, "image/") != NULL) {
/* do other things */
}
return (0);
}
这会抛出“fetch_pull():无法倒回文件句柄:错误的文件描述符”。怎么了?
最佳答案
/tmp/curl_0
是一个管道,不是吗?您不能倒带管道。阅读的内容消失了。
而且您不能将 FILE 操作和文件描述符操作结合起来,因为 FILE 有一个额外的缓冲区,它们会提前读取。
如果 /tmp/curl_0
是 regular file , 然后用 open(const char *path, int oflag, ...)
打开一个文件描述符.调用mime_type(fd)
后,您可以先回放流,然后用fdopen(int fildes, const char *mode)
将文件描述符包装到FILE 句柄中。 .或者只是关闭文件描述符并在之后使用常规的 fopen()
:
int fd = open("/tmp/curl_0", O_RDONLY);
if (fd == -1) {
perror("Could not open file");
return -1;
}
char *mime = mime_type(fd);
/***** EITHER: */
close(fd);
FILE *file = fopen("/tmp/curl_0", "r");
/***** OR (the worse option!) */
if (lseek(fd, 0, SEEK_SET) == -1) {
perror("Could not seek");
return -1;
}
FILE *fdopen = fopen(fd, "r");
/***********/
if (!file) {
perror("Could not open file");
return -1;
}
/* do things */
fclose(file);
关于c - fseek 在打开的文件指针上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26021990/
我正在运行 Windows 7 - 64 位,最新的 XAMPP 版本具有 32 位 PHP 版本。 关于测试http://php.net/manual/en/function.fseek.php#1
我在代码中遇到一种情况,其中有一个巨大的功能,可以逐行解析记录,验证并写入另一个文件。 如果文件中有错误,它将调用另一个函数来拒绝记录并写入拒绝原因。 由于程序中的内存泄漏,它会随着SIGSEGV崩溃
这不是一个纯粹的编程问题,但是它会影响使用fseek()的程序的性能,因此了解其工作原理非常重要。一点免责声明,以便它不会被关闭。 我想知道在文件中间插入数据的效率如何。假设我有一个具有1MB数据的文
在最后一个 while 循环中,应打印文件中存储的每 5 个字符,但循环无限期地进行并且不会终止。feof() 函数应该在到达 END OF FILE 时返回 1,并且应该退出循环,但循环会无限期地进
代码的作用:我读取一个二进制文件并对其进行排序。我使用频率阵列来做到这一点。更新:它确实执行了循环,但它没有正确地写入数字...... 这就是代码。我想在读取文件后写入文件。我会在已经写好的内容上加上
我想像这样计算文件字符: #include #include int main(){ int d=0; char filename[25]; FILE *file;
我想从 file.txt 中读入缓冲区几行。 目前,我fseek 和fread 循环中的每一行。我知道这些线是什么,因为我保留了它们的索引列表。因此,如果我想读取第 5 行到第 10 行,我就确切地知
我正在文件内容中搜索字符串,并通过排除空格“”将整个内容存储在 buff char[] 中,最后将此 buff char[] 与用户输入字符串进行比较以检查可用性。 但是我无法存储整个文件内容,因为
我写了这段代码,函数 fseek() 似乎有问题。因为它会影响文本文件的读取。它使函数 fscanf() 从文件中读取诸如垃圾和错误数据之类的内容。我的文件只包含 float 。 FILE *fptr
这个问题已经有答案了: fseek on a position beyond EOF does not trigger EOF using feof, how come? (4 个回答) 已关闭 8
底部解决方案 我正在开发一个 IP 转发程序。读取一个 header 字段后,我使用 fseek() 将文件指针指向下一个 IP header 字段的开头。只是,我的当前位置值为 20,偏移量为 40
我使用 fseek 和 fread 函数读取文件的指定 block ,然后将其写入另一个文件。由于某种原因,在目标文件中,我在其中写入的每个 block 之间有大约 20 个字节的重叠。 有人可以帮我
使用以下代码,我尝试从文件中读取 4 个字节: FILE *f uint32_t read_program(int A) { long i; uint32_t strofprog;
我有一个具有以下结构的 .dat 文件: Object name | Area | Length |Form Factor 我正在尝试查找哪个对象具有最小面积。我读取了整个文件并使用 ftell()
我正在制作一个 C++ 安装程序,我已将要提取的文件和要在程序中提取的文件的 8 字节文件大小附加到可执行文件中。我的程序因文件读取错误而退出,出了什么问题?请注意,除了我今天学到的知识外,我对 c
我在使用 fseek 时遇到了问题。我有一个包含获取的 HTTP 数据的文件指针。然后我让 libmagic 确定文件的 mime 类型,然后想倒带: char *mime_type (int fd)
这个问题在测验中弹出: 用正确的数字替换“Z”以将 FILE 指针“fp”设置为文件的最后一个字节: fseek( fp, Z, SEEK_END ); 我的回答是 -1,因为对我来说,位于某个字节意
我知道这个问题听起来很傻,但我不知道什么时候修改我的文件指针。我刚开始学习文件在 C 中是如何工作的。我正在做一个简单的练习,我必须编写一个函数来显示文件的大小并将文件指针作为参数。这是我的功能: i
我对 C 中的 fseek() 用法有疑问。我知道它可以设置由 fopen() 创建的文件描述符的偏移位置。但是它是否也可以设置新建文件的偏移位置呢?更具体地说,我可以: FILE * pFile
我正在编写一个比较文件大小的方法(就像任何比较方法一样)。这是我的方法(我使用 void * 作为参数,因为程序的其他部分需要它): int compareFileSize(void * p1, vo
我是一名优秀的程序员,十分优秀!