This blog is under construction

Saturday 28 April 2012

fsetpos example in C

Header file:
    stdio.h

Synopsis:
     int fsetpos(FILE *stream, const fpos_t *ptr);

Description:
     It positions the file pointer (stream) to the position given by fgetpos in *ptr.  Returns 0 on success, non-zero on failure.


fsetpos function C example:


  #include<stdio.h>
  #include<string.h>
  #include<errno.h>
  int main() {
        FILE *fp;
        char *str;
        fpos_t ptr;
        int val;
        fp = fopen("mastering-c.txt", "w");
        if (fp == NULL) {
                str =  strerror(errno);
                perror(str);
                return;
        }
        fgetpos(fp, &ptr);
        fputs("Example for fsetpos... this is the 1st line\n", fp);
        fputs("Example for fsetpos\n", fp);
        fputs("Example for fsetpos\n", fp);
        printf("Do you want write at the beginnig of the the file(0/1):");
        scanf("%d", &val);

        if(val == 1) {
                fsetpos(fp, &ptr);
                fputs("Writing at the beginning of the file\n", fp);
        } else if (val == 0) {
                fputs("Writing at the end of the file\n", fp);
        }

        fclose(fp);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
  Do you want write at the beginnig of the the file(0/1):1
  jp@jp-VirtualBox:~/cpgms/exp$ cat mastering-c.txt
  Writing at the beginning of the file
  t line
  Example for fsetpos
  Example for fsetpos
  jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
  Do you want write at the beginnig of the the file(0/1):0
  jp@jp-VirtualBox:~/cpgms/exp$ cat mastering-c.txt
  Example for fsetpos... this is the 1st line
  Example for fsetpos
  Example for fsetpos
  Writing at the end of the file



No comments:

Post a Comment