fseek in c | fseek function in c | fseek syntax in c | ftell function in c

In this article fseek in c we give the information about file handling function in C such as fseek function in c, getw and putw in c, ftell function in c  and fseek syntax in c.

fseek() – File Handling

The file pointer is set at the given position with the fseek() function.

Syntax for fseek()

fseek(file_pointer, offset, position);

  • file_pointer: The pointer of the file to which the file is to be read and written.
  • Offset: How many characters or bytes have to be replaced or stored from the beginning(SEEK_SET), end(SEEK_END) and current(SEEK_CUR) position of the file, that integer value comes here.
  • Position: Where to replace or store the data of the file, this is the position.

for eg. SEEK_SET(0), SEEK_CUR(1), SEEK_END(2)

sample.txt

Hello World

Source Code:

#include <stdio.h>

int main()

{

FILE *fptr;

char str[20];

fptr = fopen(“C:\\sample.txt”,”r+”);

fgets(str, 20, fptr);

printf(“Old file contents : %s\n”, str);   // Old contents : Hello World

fseek(fptr, 6, SEEK_SET);   // replace contents after 6 characters

fputs(“Friends”, fptr);     // put Friends after 6 characters

fclose(fptr);    //file is closed

fptr = fopen(“sample.txt”,”r”);   // file is reopened

// User can use rewind() function instead of reopening file

fgets(str, 20, fptr);

printf(“New file contents : %s\n”, str);

fclose(fptr);

return 0;

}

Output:

Old file contents: Hello World

New file contents: Hello Friends

getw() and putw() – File Handling

putw()

Integer value is written to the file with putw() function.

Syntax for putw()

putw(integer, file_pointer);

  • Integer: The integer value or its variable to be written in the file comes here.
  • file_pointer: the file in which the integer value is to be written, that file pointer comes here.

getw()

Integer value is read from the file with the getw() function.

Syntax for getw()

getw(file_pointer);

  • file_pointer : the pointer of the file whose integer is to be read.

Example for putw() and getw()

Source Code:

#include <stdio.h>

int main()

{

FILE *fptr;

int num = 5;

fptr=fopen(“C:\\file.txt”,”w”);

putw(num,fptr);

printf(“put value : %d\n”,num);

fclose(fptr);

fptr=fopen(“file.txt”,”r”);  // file is reopened

int num1 = getw(fptr);

printf(“get value : %d\n”,num1);

fclose(fptr);

return 0;

}

Output:

put value : 5

get value : 5

ftell() – File Handling

The ftell() function returns the current position of the File Pointer.

Syntax for ftell()

ftell(file_pointer);

  • file pointer: This is a file pointer. By which the current position of the file will be understood.
  • It returns long integer. With this, the data of the file is measured in bytes. If for some reason there is a problem in opening the file, then it returns -1.

Source Code:

#include <stdio.h>

int main()

{

FILE *fptr;

char str[] = “Hello World”;

fptr=fopen(“C:\\file.txt”,”w”);

fputs(str,fptr);

printf(“File Contents : %s\n”,str);

int size = ftell(fptr);

printf(“Size of file in bytes : %d\n”, size);

fclose(fptr);

return 0;

}

Output:

File Contents: Hello World

Size of file in bytes: 11

rewind() – File Handling

The rewind() function brings the file pointer to the beginning of the file.

Syntax for rewind()

rewind(file_pointer);

file pointer: The file which is opened in this file pointer, brings that file pointer to the beginning.

Source Code:

#include <stdio.h>

int main()

{

FILE *fptr;

char str[20];

fptr = fopen(“C:\\file.txt”,”r+”);

fgets(str, 20, fptr);

printf(“Old file contents : %s\n”, str);

fseek(fptr, 6, SEEK_SET);

fputs(“Friends”, fptr);

rewind(fptr);   // file is reopened

fgets(str, 20, fptr);

printf(“New file contents : %s\n”, str);

fclose(fptr);

return 0;

}

Output:

Old File Contents: Hello World

New file Contents: Hello Friends

Related Link:

Some More: DBMS/ WT/ DMDW

Santosh Nalawade

Work as Assistant Professor and Web Developer.

One thought on “fseek in c | fseek function in c | fseek syntax in c | ftell function in c

Leave a Reply

Your email address will not be published. Required fields are marked *