Lab IV File Handling
Lab IV File Handling
Q1 Q2 Q3 Total
File handling
10 10 10 30
1. This practice lab covers the following one week of lectures in the Syllabus. You are expected to watch these videos prior
to doing this lab.
Write this following program and compile it. If there are any errors, then fix them yourself 😊:
#include <stdio.h>
#include <time.h>
void fun()
{
printf("fun() starts\n");
printf("Press enter to stop fun \n");
while(1)
{
if ( getchar() )
break;
}
printf("fun() ends \n");
}
// The main program calls fun() and measures time taken by fun()
int main()
{
// Calculate the time taken by fun()
clock_t t1, t2;
t1 = clock();
fun();
t2 = clock() - t1;
double time_taken = ( (double) t2) /CLOCKS_PER_SEC; // in seconds
There are two extremely important arguments of the main function, namely, argc and argv[]. Here
‘argc’ stands for argument count and ‘argv’ stands for argument values. These are variables passed to the
main function when it starts executing. When we run a program we can give arguments to that program
like:
C:\Users\MyComputer\Downloads> Hello.exe 7
Here the argument 7 is directly provided in the console and the program will print Hello a total of
seven times. Consider the code mentioned below. Notice, now we are evaluating the execution time by
default. Evaluating the execution time should appear in all your Data-Structure codes. Write the code
below and save it as “Hello.c”
#include <iostream>
#include <bits/stdc++.h>
#include <string>
#include <ctime>
#include <stdio.h>
#include <time.h>
Save the above program as “Hello.c”. Compile it on the console and run it directly. See how the output
looks like.
DST teaches you how to handle data using programming structures. The data
encompassed within these structures is huge and goes way beyond asking the user to input
details using the keyboard. Hence, reading input from an external file is fundamental. This lab
teaches how to read an external file while storing the contents in another file.
Focus on the comments, they are mentioned specifically for your understanding. As
students are very keen in simply copy-pasting code, the following presents snap-shots (images of
the running code) so that you write these codes yourself.
Code:
Output:
You are provided a file titled ‘input.txt’ that contains random words. Make sure this file is
present in the same folder as the executable version of this program. Type in the code and see
how the code works. Run the program in the console.
S. No. Content Meets Criteria (1) Marks Does not meet expectations (0) Marks