Module 5 Important Questions
Module 5 Important Questions
fseek() is used to set the file position indicator to a specified offset from the specified
origin.
Syntax:
int fseek(FILE *stream, long offset, int origin);
stream: It is a pointer to the FILE stream.
offset: It specifies the number of bytes to offset from the origin.
origin: It indicates the position from where the offset is calculated. It can be one of the
following:
SEEK_SET: Beginning of the file.
SEEK_CUR: Current position of the file pointer.
SEEK_END: End of the file.
Return Value
It returns 0 if successful, and a non-zero value if an error occurs.
2. rewind() Function
rewind() is used to move the file pointer to the beginning of the file stream.
Syntax:
void rewind(FILE *stream);
Parameters
stream: It is the pointer to the file stream
Return Value
It does not return any value.
3.ftell() function
ftell() in C is used to find out the position of the file pointer in the file with respect to
starting of the file.
Syntax:
Parameters
stream: It is the pointer to the file stream.
Return Value
4. fread() function
The C fread() is a standard library function used to read the given amount of data from a
file stream. Defined inside <stdio.h>, the fread() function reads the given number of
elements of specific size from the file stream and stores it in the buffer memory. The total
number of bytes read by fread() function is the number of elements read multiplied by the
size of each element in bytes.
Syntax:
size_t fread(void * buffer, size_t size, size_t count, FILE * stream);
The file position indicator is automatically moved forward by the number of bytes read. If
the objects being read are not trivially copy-able, such as structures or complex data types
then it does not behave properly.
Parameters
buffer: It refers to the pointer to the buffer memory block where the data read will be
stored.
size: It refers to the size of each element in bytes.
count: It refers to the count of elements to be read.
stream: It refers to the pointer to the file stream.
5. fwrite() function
We can use fwrite() function to easily write a structure in a file. fwrite() function writes the
to the file stream in the form of binary data block.
Syntax of fwrite()
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
Parameters
Return Value
6. feof() Function
The feof() function is used to check whether the file pointer to a stream is pointing to the
end of the file or not. It returns a non-zero value if the end is reached, otherwise, it returns
0.
Syntax of feof()
int feof(FILE* stream);
int main() {
FILE *file;
char buffer[100];
return 0;
}
⭐2. Differentiate between sequential files and random access files?(3 marks)
In this example, `ptr` is a pointer to an integer that will be used to manipulate the value at
the memory location pointed to by the pointer.
⭐⭐4. Explain different modes of opening files with an example. (3 marks or 7 marks)
Ans:
} //Similarly replace r by w,a,r+ etc.. according to needs to open file in different modes.
5. Distinguish between text files and binary files.(3 marks)
⭐⭐6. Distinguish between text mode and binary mode operation of a file.( 3 marks)
⭐7. Differentiate between array of pointers and pointer to an array. (7 marks)
Ans: **Array of Pointers:**
- An array of pointers is an array in which each element is a pointer to another data object.
- Each element of the array holds the memory address of another data object.
- It's often used to represent a collection of data objects that are dynamically allocated or
have varying sizes.
- Useful when we need to work with multiple objects of different sizes or types.
- The array size is fixed, but the individual pointers in the array can point to different
memory locations.
Example:
int *ptrArray[5]; // An array of 5 pointers to integers
char *strArray[3]; // An array of 3 pointers to strings
**Pointer to an Array:**
- A pointer to an array is a single pointer that points to the entire array.
- It's used to access and manipulate an entire array as a single entity.
- Useful when you want to pass arrays to functions or when you want to encapsulate array
manipulation.
- The pointer holds the memory address of the array's first element.
- The size of the array can be deduced from the type information stored in the pointer.
Example:
int arr[5]; // An array of 5 integers
int (*ptr)[5]; // A pointer to an array of 5 integers
In summary:
- **Array of Pointers:** An array where each element is a pointer to another data object.
The array stores multiple pointers.
- **Pointer to an Array:** A pointer that points to the entire array. It allows treating the
array as a single entity.
⭐⭐8. Differentiate between indirection and address of operator (3 marks)
Ans: **Indirection Operator (`*`):**
- The `*` operator is used to access the value pointed to by a pointer.
- It allows you to retrieve and modify the data stored at a memory location pointed to by the
pointer.
Example:
int x = 42;
int *ptr = &x; // Pointer 'ptr' holds the address of 'x'
int value = *ptr; // Dereferencing 'ptr' to get the value of 'x' (42)
**Address-of Operator (`&`):**
- The `&` operator is used to obtain the memory address of a variable.
- It's useful for initializing pointers or passing memory addresses to functions.
Example:
int y = 77;
int *ptr = &y; // Pointer 'ptr' holds the address of 'y'
⭐9. Write a C program to count number of lines and words in a text file. (7 marks)
Ans: #include<stdio.h>
#include<string.h>
void main()
{
FILE *f1,*f2;// file pointers
int countW=0,countL=0;
char ch;
f1 = fopen("test.txt","r"); //open file in read mode
while (feof(f1) == 0 )
{
ch = fgetc(f1);// to get character by character from text file
if(ch == ' ' || ch == '\t' || ch == '\n')//checking for white spaces
countW++; //count no of words
if(ch == '\n')
countL++; //count no of lines
}
printf("Count of words = %d\n",countW);
printf("Count of Lines = %d",countL);
fclose(f1);
}
10. What do you mean by a pointer variable? How is it initialised? (3 marks)
Ans: A pointer variable in C stores the memory address of another variable. It allows
indirect access to the value at that memory location. Initialization is done by assigning the
memory address of a variable using the address-of operator (`&`).
int num = 42;
int *ptr = # // 'ptr' points to the memory address of 'num'
int value = *ptr; // Accesses the value of 'num' through 'ptr'
⭐⭐11. Write a C program to print the elements of an array in reverse order using
pointers.( 7 marks)
Ans: #include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
// Input elements from the user
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Point to the last element of the array
int *ptr = arr + size - 1;