mja
mja

Reputation: 1365

About the FILE * streams and how fputc() works

I wonder about the operation of FILE pointer f and how the function fputc works.

First, when I open a file (I have not been working on it yet, like writing or reading). What position of f in the file? Is it before the first character?

Second, when I use:

fseek(f, -1, SEEK_CUR);
fputc(' ', f);

what position of my pointer f now?

Upvotes: 1

Views: 1112

Answers (4)

Mike
Mike

Reputation: 167

It depends on your current position/offset for an example if your file pointer was on 100th offset and you write fseek(f, -1, SEEK_CUR); and the offset will be at 99th position, and then you write space on 99th position, after writing space using fputc(' ', f); file pointer's offset will be 100th again.

Upvotes: 0

Dhvanish Thakkar
Dhvanish Thakkar

Reputation: 77

  • When accessing files through C, the first necessity is to have a way to access the files. For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed. For Example:

     FILE *fp;
    
  • To open a file you need to use the fopen function, which returns a FILE pointer. Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file.

    FILE *fopen(const char *filename, const char *mode);
    
  • Here filename is string literal which you will use to name your file and mode can have one of the following values

    w  - open for writing (file need not exist)
    a   - open for appending (file need not exist)
    r+ - open for reading and writing, start at beginning
    w+ - open for reading and writing (overwrite file)
    a+ - open for reading and writing (append if file exists)
    
  • Following is the declaration for fseek() function.

    int fseek(FILE *stream, long int offset, int whence)
    
    SEEK_SET    Beginning of file
    SEEK_CUR    Current position of the file pointer
    SEEK_END    End of file
    
  • Following fputc() example :

    /* fputc example: alphabet writer */
     #include <stdio.h>
    
     int main ()
     {
        FILE * pFile;
         char c;
    
        pFile = fopen ("alphabet.txt","w");
         if (pFile!=NULL) {
    
          for (c = 'A' ; c <= 'Z' ; c++)
          fputc ( c , pFile );
    
          fclose (pFile);
        }
        return 0;
       }
    

Upvotes: 1

chqrlie
chqrlie

Reputation: 145277

When you open the file, the current position is 0, at the first character.

If you try to fseek before the beginning of the file, fseek will fail and return -1.

Note that if you seek backwards on a text file, there is no guarantee that is can succeed. On linux and/or for a binary stream, assuming you are not at the start of the stream, opened in write mode for a real file, after the sequence

fseek(f, -1L, SEEK_CUR);
fputc(' ', f);

the position of the stream will be the same as before the fseek.

But consider this seemingly simpler example:

fputc('\n', f);
fseek(f, -1L, SEEK_CUR);

On systems such as Windows, where '\n' will at some point be converted into a sequence of 2 bytes <CR><LF>, what do you think it should do?

Because of all these possibilities for failure (and a few more exotic ones), you should always test the return value of fseek and try to minimize its use.

Upvotes: 1

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36431

Reading the manuals should help you.

For fopen: the stream is positioned at the beginning of the file. Except for mode like 'a'

For fseek: that function can fail, you have to test the return value; and it is not difficult to imagine that you cannot obtain a negative offset.

Upvotes: 2

Related Questions