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

FoCP Lab Manual 13

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)
16 views

FoCP Lab Manual 13

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

Department of Computing

CS-110 Fundamentals of Computer Programming

Lab Manual 13: File Handling


CLO-2 Solve a given real-world problem by applying appropriate programming concepts and
techniques.

CLO-3 Demonstrate the ability to independently understand and solve a problem using
programming techniques and apply the acquired knowledge to challenging tasks.

CLO-4 Use the latest IDEs and other supplementary tools to aid code implementation, testing, and
management as per standard practices in the software industry.

BSCS-13E
Time: 14:30 -17:00
Date: 22-12-2023
Course Instructor’s: Dr. Naseer Bajwa
Lab Engineer: Engr. Masabah Bint E Islam
Evaluation Rubric

S. No. Assessment Meets expectation(3/4 Exceeds expectation(5


Unacceptable Does not meet marks) marks)
(0 marks) expectation (1/2marks)

Application of The student is unable to apply The student requires some The student demonstrates a
1
Programming guidance to apply the clear ability to apply the
Concepts the appropriate programming appropriate programming appropriate programming
concepts to solve the given concepts to solve the given concepts to solve the given
(CLO 2, PLO 3)
problem. The program flow problem. The program flow
problem thus resulting in an diagrams and pseudocodes diagrams and pseudocodes
incomplete or ineffective require minor are adequate. The code has
improvements. The code sufficient comments and
solution. The program flow has limited comments and consistent variable names
diagrams and pseudocodes inconsistent variable names and reasonably adheres to
and may not adhere to the the coding standards.
are missing or coding standards.
incomprehension. The code
has inadequate comments
and variable names and does
not adhere to the coding
standards.
The student did
Software tool The student has no idea The student has a limited The student has full
2 not submit any
usage how to use the basic tools command on various tools
word OR The command of the basic tools
of the software. The codes available in the software.
(CLO 3, PLO4) student
have syntax errors, and of the software. The codes Furthermore, his/her coding
plagiarized the
parts of the coding are is complete and functional,
solution and/or are correct in terms of their
missing. Codes are and the program output is
used unfair
nonmodular and cannot be syntax; however, the program correct. Codes are modular,
means.
reused. reusable, and easily
output is not always correct. readable.
Codes are semi-modular and
semi-reusable.

Individual and The student disturbs the The student cooperates with In addition to performing
3
Teamwork (ClO- lab environment. Distracts other group members in a well individually and in a
4, PLO-5) or discourages other reasonable manner. Performs team, also provided
group members from well individually and in a leadership in the team.
conducting the team. Actively engages and
experiments. Can’t cooperates with other group
perform well both members in an effective
individually and as part of manner.
a team.
Introduction to Experiments
In this fundamental of computer programming lab manual, we explore the
file handling in C.

Objectives of the Experiment


Upon completing this section, you will be capable of developing programs with
using file handling in C.

Tools/Software Required
• IDE for C Language

Introduction to File Handling in C


The ability to work with files is a fundamental skill in programming, allowing you to store and retrieve data,
manipulate file contents, and perform various operations on files. In this section, we will guide you through a series
of tasks that will help you grasp these concepts effectively. In C programming, fopen, fclose, fprintf, and similar
functions are part of the standard I/O library and are used for file handling. Here's an overview of each of these
functions:

1. fopen()
The fopen function is used to open a file.

Syntax: FILE *fopen(const char *filename, const char *mode);


Parameters:
• filename: The name of the file to be opened.
• mode: The mode in which the file should be opened. Common modes include:
• "r": Open a file for reading. The file must exist.
• "w": Open a file for writing. If the file exists, its contents are overwritten. If the file doesn't exist, it will be
created.
• "a": Open for appending. Data written to the file is added to the end. The file is created if it doesn't exist.
• "r+": Open for both reading and writing. The file must exist.
• "w+": Open for both reading and writing. If the file exists, its contents are overwritten. If it doesn't, a new
file is created.
• "a+": Open for both reading and appending. The file is created if it doesn't exist.
• Return Value: If successful, fopen returns a pointer to the FILE object that represents the open file. If it
fails, it returns NULL.
2. fclose()
The fclose function is used to close an open file.
Syntax: int fclose(FILE *stream);
Parameters:
• stream: A pointer to the FILE object that represents the open file.
• Return Value: Returns zero on success, or EOF (end of file) if an error occurs.
3. fprintf()
The fprintf function is used to write formatted output to a file.
Syntax: int fprintf(FILE *stream, const char *format, ...);
Parameters:
• stream: A pointer to the FILE object where the output is written.
• format: A string that specifies the format of the output. It can include format specifiers that are replaced
by the values of the additional arguments.
• Additional arguments, each containing one value to replace a format specifier in the format string.
• Return Value: It returns the total number of characters written, or a negative number if an output error
occurs.
Example Code:

#include <stdio.h>
int main() {
FILE *fp;
// Open file for writing
fp = fopen("example.txt", "w");
if (fp == NULL) {
perror("Error opening file");
return -1;
}
// Write to the file
fprintf(fp, "Hello, world!\n");
// Close the file
fclose(fp);
return 0;
}

In this example, a file named "example.txt" is opened for writing. If the file opening is successful, "Hello, world!" is
written to the file. After writing, the file is closed using fclose. Remember to always check if fopen returns NULL,
as this indicates an error in opening the file (such as the file not existing or lacking the necessary permissions).
Please refer to course slides for more information.

Lab Tasks

Task 1: Write to a File


Objective: Write a C Program to open a file and write a line of text in it by reading the text from
the keyboard.

Description:

1. Prompt the user to enter a line of text.


2. Open a file in write mode.
3. Write the user's input to the file.
4. Close the file.

Task 2: Read a File


Objective: Write a program to illustrate how a file stored on the disk is read.
Description:

1. Ask the user for the name of the file to read.


2. Open the specified file in read mode.
3. Read the file's contents and display them on the screen.
4. Close the file.

Task 3: Merge File Contents


Objective: Write a C Program to read the contents of file 'File1' and paste the contents at the
beginning of another file 'File2'.

Description:

1. Open 'File1' in read mode and 'File2' in read and write mode.
2. Read the contents of 'File1'.
3. Read the contents of 'File2' and temporarily store them.
4. Write the contents of 'File1' to 'File2', then append the original contents of 'File2'.
5. Close both files.

Task 4: Count Characters and Spaces


Objective: Write a C Program to count the number of characters and spaces in the input
supplied from a file.

Steps:

1. Prompt the user for the name of the file to analyze.


2. Open the specified file in read mode.
3. Read the file's contents, counting the characters and spaces.
4. Display the counts to the user.
5. Close the file.

Deliverables:
Submit a single file on LMS before the due date in PDF format as communicated by lab engineer.

Note: Use proper indentation and comments. Lack of comments and indentation will result in
deduction of marks. You will submit your working codes and outputs (Just Screenshots).

You might also like