如何在不移动到下一个文件指针位置的情况下获取文件指针值(在 C 中)
有没有一种方法可以在不使用 fgetc 的情况下找到存储在文件指针中的字符。 假设我在一个名为 apple.txt 的文件中有一个单词“steve jobs”,是否有类似的内容
int main(int argc,char *argv[])
{
FILE *fp=fopen("apple.txt","r");
if(fp=='s'&&(fp+2)=='e')
printf("steve is there");
else
printf("Steve not there");
fclose(fp);
}
显然这实际上不起作用!它只是打印类似 C 禁止比较 b/n 指针 n int 的内容。即使我尝试在 (fp+2) 和 fp 前面添加 *,它也表示与运算符==不匹配
我相信函数 fgetc(FILE *) 的工作方式是获取无符号字符,然后将指针移动到下一个位置。是否有任何函数可以在不移动指针的情况下获取字符,并且上述代码是否可以以任何其他方式实现?!
Is there a way to find the character stored in file pointer without fgetc.
Say I have a word "steve jobs" in a file called apple.txt is there something like
int main(int argc,char *argv[])
{
FILE *fp=fopen("apple.txt","r");
if(fp=='s'&&(fp+2)=='e')
printf("steve is there");
else
printf("Steve not there");
fclose(fp);
}
Clearly this doesn't really work! It just prints something like C forbids comparison b/n pointer n int. Even i tried adding * in front of (fp+2) and fp, it said no match for operator==
I believe the function fgetc(FILE *) works in such a way that it gets the unsigned char and then moves the pointer to the next location. Is there any function to just get the character without moving the pointer and also is the above code possible in any other way?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“存储在文件指针中的字符”并不是一个有意义的要求。
FILE
中不一定存储任何字符。例如,stdin 和 stdout 通常是通信通道而不是磁盘上的文件,并且它们可能根本不进行任何缓冲。如果您知道
FILE
指的是磁盘上的实际文件而不是通信通道,那么您可以做的是读取几个字符,然后使用fseek
返回到阅读它们之前的位置。您的示例程序,重写为做到这一点,看起来像这样:但是,如果您正在使用一个您不知道其性质的文件 - 例如由用户 - 您必须做好
fseek
失败的准备,并将errno
设置为ESPIPE
,这意味着“这不是一个文件,而是一个数据”流,你不能在里面跳来跳去”。"The character stored in file pointer" is not a meaningful thing to ask for. There aren't necessarily any characters stored in a
FILE
. For instance,stdin
andstdout
are often communication channels rather than files on the disk, and they may not be doing any buffering at all.What you can do instead, if you know a
FILE
refers to an actual file on the disk rather than a communications channel, is read a few characters and then usefseek
to move back to where you were before you read them. Your example program, rewritten to do that, would look something like this:If you're working with a file whose nature you don't know, though -- such as any file given to you by the user -- you have to be prepared for
fseek
to fail and seterrno
toESPIPE
, which means "that's not a file, that's a data stream, you can't jump around in it".您可以使用
getc
和ungetc
。阅读有关它的文档(您只能有一个后推字符)。在某些系统(特别是像 Linux 这样的 Posix 系统)上,您可以将文件的一部分映射到内存中。详细了解
mmap
系统调用,它使您能够查看文件的一部分作为内存段。 (mmap
不适用于不可查找的文件,如管道或套接字;它基本上主要适用于“磁盘”文件)。You might use
getc
andungetc
. Read the documentation about it (you can only have one push-back character).On some systems (notably Posix systems like Linux) you have the ability to map into memory a portion of a file. Read more about the
mmap
system call which enables you to see a portion of a file as segment of memory. (mmap
is not usable on non-seekable files like pipes or sockets; it basically works mostly on "disk" files).