如何在不移动到下一个文件指针位置的情况下获取文件指针值(在 C 中)

发布于 2024-12-12 16:05:46 字数 512 浏览 0 评论 0原文

有没有一种方法可以在不使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半寸时光 2024-12-19 16:05:51

“存储在文件指针中的字符”并不是一个有意义的要求。 FILE 中不一定存储任何字符。例如,stdin 和 stdout 通常是通信通道而不是磁盘上的文件,并且它们可能根本不进行任何缓冲。

如果您知道 FILE 指的是磁盘上的实际文件而不是通信通道,那么您可以做的是读取几个字符,然后使用 fseek 返回到阅读它们之前的位置。您的示例程序,重写为做到这一点,看起来像这样:

int main(void)
{
    FILE *fp = fopen("orange.txt", "r")
    if (!fp)
    {
        perror("fopen(orange.txt)");
        return 1;
    }

    if (getc(fp) == 'b' &&
        getc(fp) == 'a' &&
        getc(fp) == 'n' &&
        getc(fp) == 'a' &&
        getc(fp) == 'n' &&
        getc(fp) == 'a')
        puts("orange has a banana");
    else
        puts("orange has no bananas");

    /* return to the beginning of the file */
    if (fseek(fp, 0, SEEK_SET))
    {
        perror("fseek(orange.txt)");
        return 1;
    }

    /* do something else with `fp` here */

    /* all files are automatically closed on exit */
    return 0;
}

但是,如果您正在使用一个您不知道其性质的文件 - 例如由用户 - 您必须做好 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 and stdout 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 use fseek to move back to where you were before you read them. Your example program, rewritten to do that, would look something like this:

int main(void)
{
    FILE *fp = fopen("orange.txt", "r")
    if (!fp)
    {
        perror("fopen(orange.txt)");
        return 1;
    }

    if (getc(fp) == 'b' &&
        getc(fp) == 'a' &&
        getc(fp) == 'n' &&
        getc(fp) == 'a' &&
        getc(fp) == 'n' &&
        getc(fp) == 'a')
        puts("orange has a banana");
    else
        puts("orange has no bananas");

    /* return to the beginning of the file */
    if (fseek(fp, 0, SEEK_SET))
    {
        perror("fseek(orange.txt)");
        return 1;
    }

    /* do something else with `fp` here */

    /* all files are automatically closed on exit */
    return 0;
}

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 set errno to ESPIPE, which means "that's not a file, that's a data stream, you can't jump around in it".

不必了 2024-12-19 16:05:50

您可以使用 getcungetc。阅读有关它的文档(您只能有一个后推字符)。

在某些系统(特别是像 Linux 这样的 Posix 系统)上,您可以将文件的一部分映射到内存中。详细了解 mmap 系统调用,它使您能够查看文件的一部分作为内存段。 (mmap 不适用于不可查找的文件,如管道或套接字;它基本上主要适用于“磁盘”文件)。

You might use getc and ungetc. 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文