0% found this document useful (0 votes)
21 views11 pages

Module 5 Important Questions

The document discusses various file handling functions in C, including fseek(), rewind(), ftell(), fread(), fwrite(), and feof(), detailing their syntax, parameters, and return values. It also explains the concept of passing pointers to functions, the distinction between sequential and random access files, and the differences between text and binary files. Additionally, it covers topics like array of pointers vs pointer to an array, indirection vs address-of operator, and provides example programs for counting lines and words in a file and printing array elements in reverse order.

Uploaded by

akbarsha3336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views11 pages

Module 5 Important Questions

The document discusses various file handling functions in C, including fseek(), rewind(), ftell(), fread(), fwrite(), and feof(), detailing their syntax, parameters, and return values. It also explains the concept of passing pointers to functions, the distinction between sequential and random access files, and the differences between text and binary files. Additionally, it covers topics like array of pointers vs pointer to an array, indirection vs address-of operator, and provides example programs for counting lines and words in a file and printing array elements in reverse order.

Uploaded by

akbarsha3336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

MODULE -5

POINTERS AND FILES

⭐⭐1. Explain any 5 file handling functions in C. (7 marks)

Ans: 1. fseek() Function

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:

long ftell(FILE *stream);

Parameters
 stream: It is the pointer to the file stream.

Return Value

 It returns a long integer value as the current position in the file.


 It returns -1 if an error occurs.

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

 ptr: pointer to the block of memory to be written.


 size: the size of each element to be written (in bytes).
 nmemb: umber of elements.
 stream: FILE pointer to the output file stream.

Return Value

 Number of objects written.

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);

Example program covering these functions with comments:


#include <stdio.h>

int main() {
FILE *file;
char buffer[100];

// Open a file in write mode


file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening the file.\n");
return 1;
}

// Write data to the file


fprintf(file, "Hello, World!\nThis is a simple example.");

// Close the file


fclose(file);

// Open the file in read mode


file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening the file.\n");
return 1;
}

// Move the file pointer to the beginning using rewind()


rewind(file);

// Read and print data from the file


while (!feof(file)) {
fgets(buffer, sizeof(buffer), file);
printf("%s", buffer);
}
// Get the current position of the file pointer using ftell()
long position = ftell(file);
printf("Current position: %ld\n", position);

// Move the file pointer to a specific position using fseek()


fseek(file, 7, SEEK_SET); // Move to the 8th byte (0-based index) from the
beginning

// Read and print data from the new position


fgets(buffer, sizeof(buffer), file);
printf("After fseek(): %s", buffer);

// Close the file


fclose(file);

return 0;
}

⭐2. Differentiate between sequential files and random access files?(3 marks)

⭐⭐3. Explain how pointers can be passed to functions in C. (7 marks)


OR
With a suitable example, explain the concept of pass by reference.(7 marks)
Ans: In C, pointers can be passed to functions as arguments, which allows the function to
operate on the data pointed to by the pointer rather than on a copy of the data. This
method is called pass by reference method in C.
Here's how we can pass pointers to functions in C:
1. **Defining the Function Parameter:**
When we want to pass a pointer to a function, we need to define the function parameter
as a pointer type. For example:
void modifyValue(int *ptr) {
// Function code
}

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.

2. **Passing the Pointer:**


When calling the function, we pass the memory address of the data we want to work with.
This is typically done using the `&` (address-of) operator when passing variables or arrays.
For example:
int main() {
int num = 42;
modifyValue(&num); // Passing the address of 'num'
// Rest of the code
return 0;
}

3. **Accessing and Modifying Data:**


Inside the function, you can use the pointer to access and modify the data it points to. To
access the data, you can use the dereference operator `*`. For example:
void modifyValue(int *ptr) {
*ptr = 99; // Modifying the value at the address pointed to
by 'ptr'
}
When we pass a pointer to a function, we are working directly with the memory location
that the pointer points to. This can lead to efficient code since we're not copying data
around, but it also requires careful handling to ensure we don't accidentally access or
modify the wrong memory locations, which could result in undefined behavior or crashes.

⭐⭐4. Explain different modes of opening files with an example. (3 marks or 7 marks)
Ans:

Eg: #include <stdio.h>


int main() {
FILE *file;
// Opening a file in "r" mode (Read)
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening the file.\n");
return 1;

} //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

ptr = &arr; // Assign the address of the array to the pointer

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 = &num; // '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;

// Print the elements in reverse order using the pointer


printf("Elements in reverse order: ");
for (int i = 0; i < size; i++) {
printf("%d ", *ptr);
ptr--;
}
return 0;
}

You might also like