需要在C fseek的()的 [英] need of fseek() in c

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

问题描述

code的第1部分: -

Part of Code 1:-

while(1)
    {
        ch=fgetc(pt);
        if(c==EOF)
        {
            break;
        }
        if(c==' ')
        {
            fputc('z',pt);
        }
    }

code的第2部分: -

Part of Code 2:-

while(1)
{
    ch=fgetc(pt);
    if(c==EOF)
    {
        break;
    }
    if(c==' ')
    {
        fseek(pt,0,SEEK_CUR);
        fputc('z',pt);
        fseek(pt,0,SEEK_CUR);
    }
}

我想在一个文件中的每个空格后更换下一个字符。该文件由指针 PT 指出。

I want to replace next character after every space in a file. That file is pointed by the pointer pt.

无论是code没有显示出错误并运行良好,但是当我打开外部的 .TXT 文件,首先code什么也没做,而第二个$ C $℃之后空格成功替换下一个字符。

Both the code shows no error and runs fine, but when I externally opens the .txt file, first code did nothing whereas the second code replaces the next character after space successfully.

显然 fseek的(PT,0,SEEK_CUR); 正在差别。

所以我无法理解它在第二code是干什么的?

So I am unable to understand that what it is doing in the second code?

推荐答案

使用的fseek的()在这里 - 在C标准要求的读取和之间的定位操作上的更新流写操作,或写入和读出之间。这是写和读出之间的定位操作。这不是一个空操作;它流放入一个模式,它允许一个龟etc()工作正常,可靠,跨平台,所要求的C标准。

The use of fseek() here - The C standard requires a positioning operation between a read and a write operation on an update stream, or between a write and a read. This is a positioning operation between a write and a read. It is not a no-op; it places the stream into a mode which allows the next fgetc() to work correctly, reliably, across platforms, as required by the C standard.

编辑:

2 fseek的()调用必需的,因为第一次充当空操作呼叫的龟etc之间()和随后的的fputc()电话。在的fputc(),第二个充当的fputc之间的不操作()和随后龟etc()电话。 (因为一个循环运行)

2 fseek() calls are required because the first one acts as the "no-op" call between an fgetc() and a subsequent fputc() call. After the fputc(), the second one acts as the "no-op" between the fputc() and the subsequent fgetc() call. (since a loop is running)

这篇关于需要在C fseek的()的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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