×

Positioning of file in C

Positioning() function in c

The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the constants SEEK_SET , SEEK_CUR , or SEEK_END , to show whether the offset is comparative  with the start of the record, the ongoing document position, or the finish of the document, individually.

seekg() function is utilized to reposition a document pointer in a record. The function takes the offset and relative situation from where we really want to move out pointer.

fseek() is utilized with its each of the 3 disagreement or specially involved in arguments alongside the other document functions to run out their particular purpose.

It is fundamentally a C function utilized for dealing with the document tasks. In C, document pointer is returned by the fopen() function(which is likewise an in-assembled function) utilized for opening any record in C and 'numbytes' is utilized to return the quantity of bytes from the document beginning.

By utilizing the fseek() capability in C, the ideal read and compose tasks can be performed by finding the document pointer and moving it appropriately.

The record position of a stream portrays where in the document the stream is right now perusing or composing.

I/O on the stream propels the record position through the document. on windows system the document position is addressed as a whole number, which counts the quantity of bytes from the very start of the record.

During I/O to a set of circumference circle record, you can change the document position at whatever point you wish, to peruse or compose any piece of the record. A few different sorts of documents may likewise allow this.

Documents which backing changing the record position are in some cases  to as irregular access documents.

For the following functions we use the positioning of files.

fseek() functions is the file handling functions in C programming language. It has following constants. SEEK_SET, SEEK_CUR, SEEK_END. Let’s see the total description and syntax for the above file handling functions.

Firstly lets see the fseek() function:

Fseek() :

he C library capability or function fseek() sets the record position of the stream to a given offset. The pointer related with the document is moved to that offset.

Syntax for the fseek() function:

Int fseek(FILE*stream, int offset, int whence);
  • Stream : points to the file object.
  • Offset: number of the bytes offset to the position.
  • Whence: the position where offset is added.

Whence defines that the point with respect to that where the file pointer needs to moved. It is specified by the following constants:

  • SEEK_END
  • SEEK_SET
  • SEEK_CUR

Lets see the following definitions:

  • SEEK_END: The seek_end function defines the end of the file pointer.
  • SEEK_SET: The seek_set function defines the beginning of the file pointer.
  • SEEK_CUR: The seek_cur function defines the current position of the file pointer

Lets see an example program for file positioning

#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("sample.txt", "r");
fseek(fp, 0, SEEK_END);
printf("Position of the file pointer is : ");
printf("%ld \n", ftell(fp));
fseek(fp,10,SEEK_SET);
int ch;
printf("the following result is: ");
while( (ch=fgetc(fp)) != EOF)
putchar(ch);
return 0;
}

Input:

Lets create a sample.text to store the input:
Lets create wonders

Output:

Position of the file pointer is : 9
The following result is: e wonders

In the above code, there is a document 'sample.txt' that as of now exists in the framework. To begin with, we are including the stdio.h library which is required to utilize fseek() function in C for any record related I/O activities.

 To start with, the record is opened utilizing the fopen() function and the document pointer is returned by it. Presently the fseek() function is utilized to move the record pointer to the furthest limit of the document.

After that the absolute number of document characters are imprinted on the control center or the place of the pointer which is at the remainder of the record is printed utilizing the 'ftell()' function.

fseek() function is again utilized to move the document pointer to the eleventh position and afterward the subsequent characters of record to attempt or ignore the initial 10 characters are imprinted on the center utilizing putchar() function.


Related Topics

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.