1

Part of Code 1:-

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

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);
    }
}

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

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.

Clearly fseek(pt,0,SEEK_CUR); is making the difference.

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

kevin gomes
  • 1,775
  • 5
  • 22
  • 30
  • Are you sure it's fputc('z', ft) and not fputc('z', pt) in the 2nd code? – AndyFaizan Feb 23 '14 at 06:56
  • @AndyFaizan : oops sorry...My mistake!!!!That was `pt` – kevin gomes Feb 23 '14 at 06:57
  • possible duplicate of [modify existing contents of file in c](http://stackoverflow.com/questions/21958155/modify-existing-contents-of-file-in-c) – Lee Duhem Feb 23 '14 at 07:05
  • @leeduhem: Not exactly but the answer there solves the issue. – AndyFaizan Feb 23 '14 at 07:12
  • 2
    @kevingomes: 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 – AndyFaizan Feb 23 '14 at 07:13
  • @AndyFaizan : I also read `Jonathan Leffler` comment, but I am asking what the presence of `fseek(pt,0,SEEK_CUR)` do in this case. – kevin gomes Feb 23 '14 at 07:15
  • Doesn't my comment answer this question? – AndyFaizan Feb 23 '14 at 07:18
  • @AndyFaizan : in your comment you stated what c standard requires. That I also know. What I am asking is that what it is doing????? – kevin gomes Feb 23 '14 at 07:21
  • 1
    From the comment - "it places the stream into a mode which allows the next fgetc() to work correctly". Otherwise it malfunctions like part 1 of your code. I guess that's about it – AndyFaizan Feb 23 '14 at 07:24
  • 1
    @AndyFaizan: You should make that an answer. – R.. GitHub STOP HELPING ICE Feb 23 '14 at 07:28

1 Answers1

2

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.

EDIT:

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)

AndyFaizan
  • 1,833
  • 21
  • 30