0% found this document useful (0 votes)
16 views

Lab IV File Handling

This practice lab covers file handling and using arguments from the main function. It contains three parts that evaluate execution time, take parameters from the console, and read/write files. Students are expected to write and run the code provided and gain experience with these core data structures concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab IV File Handling

This practice lab covers file handling and using arguments from the main function. It contains three parts that evaluate execution time, take parameters from the console, and read/write files. Students are expected to write and run the code provided and gain experience with these core data structures concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practice Lab (Week 4): Data Structures

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.

Lecture Topics Reading


7 File Handling: Using 1. Using arguments of the main function: https://youtu.be/1QzY-yMFHC8
arguments of the main 2. Reading and Writing files: https://youtu.be/15bOZuCV4i0
8 function, reading and writing
files.

This Lab contains three parts, each part having 10 marks.


Part 1) Evaluating ‘Execution time’:

The library <Time.h> has three important elements:

1. clock_t: this is our data-type,


2. CLOCKS_PER_SEC: a reserved word which evaluates the number of clocks the timer makes per second.
This variable changes for different processors. Faster processors have higher clocks per second
3. clock(): function that measures the number of clocks at a particular instance. It returns the number of
clock ticks elapsed since the program was launched. To get the number of seconds used by the CPU,
you will need to divide by CLOCKS_PER_SEC.

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

printf("fun() took %f seconds to execute \n", time_taken);


return 0;
}

Part 2) Feeding parameters directly from the console:

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>

using namespace std;


int main(int argc, char *argv[])
{
if (argc != 2)
{
cout << "This program has 2 arguments:" << endl;
cout << "[1] Name of the program: Hello.exe” << endl;
cout << "[2] Parameter - number of times you want to print Hello” << endl;
cout << endl << endl;
cout << “For e.g.: typing the following:” << endl
cout << “Hello.exe 7” << endl
cout << “Will print Hello seven times” << endl;
}
else
{
cout << “Let’s start the program” << end

// Convert string number to integer number


int length = strlen( argv[1] );
int tau = 0;
int p, o = 1 ;
for( p = len-1 ; p > -1 ; p = p - 1 )
{
tau = tau + ( (int) argv[2][p] - 48 ) * o ;
o = o * 10 ;
}

// Printing hello tau number of time


for ( int i = 1; i <= tau; i++)
cout << “[“ << i << “]” << “ Hello” << endl;
} // end of else
return 0;
} // end of main function

Save the above program as “Hello.c”. Compile it on the console and run it directly. See how the output
looks like.

Part 3) Input/Console application:

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.

Rubrics (Associated Marks)

S. No. Content Meets Criteria (1) Marks Does not meet expectations (0) Marks

1 Indentation Perfect 100% Code not indented properly 0


2 Code works Code compiles and executes properly for 100% Code has errors Based on
any variable sized matrices the code
3 Comments Code is properly commented 100% Code is not properly commented. 0

You might also like