C facilitates its programmers to pass command arguments. Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating system.
ACCEPTING
COMMAND LINE ARGUMENTS
C
facilitates its programmers to pass command arguments. Command-line arguments
are given after the name of a program in command-line operating systems like
DOS or Linux, and are passed in to the program from the operating system.
Till
now, no arguments were passed to the main(). But now in order to understand
command-line arguments, you must first understand the full declaration of the
main function. The main () can accept two arguments,
•
The first argument is an integer value that specifies number of command-line
arguments. full lis
•
The second argument is a full list of all of the command- line arguments.
The
full declaration of main () can be given as
int main (int arg c, char 9c, char
*argv[])
The
integer, arg c specifies the number of arguments passed into the program from
the command line, including the name of the program.
The
array of character pointers, argv contains the list of all the arguments. argv
[0] is the name of the program, or an empty string if the name is not
available. argv[1] to argv [argc 1] specifies the command line argument. In the
C program, every element in the argv can be used as a string. Moreover,
elements of argv can also be accessed as a two-dimensional array. Note that
argv [argc] is a null pointer.
In
other words, each element of the array argv is a pointer where each pointer
points to a string. Thus, argv [0] points to a string that contains the first
parameter on the command line which is the program's name, argv[1] points to
the next parameter, and so on. Look at the program given below which
illustrates the use of command line arguments.
int main(int argc, char *argv[])
{
int i;
printf("\n Number of argumnets
passed = %d", argc);
for (i = 0; i < argc; i++)
printf("\n arg[i] = %s",
argv[i]);
return 0;
}
In
the program, the main () accepts command line argu- ments through argc and
argv. In the main() function, the value of argc is printed which gives the
number of arguments passed. Then each argument passed is printed in the for
loop using the array of pointers, argv.
For example when you execute this
program from dos prompt by writing
C:\>tc clpro.c Reema Thareja
Then argc = 3, where argv [0] ==
clpro.c
argv[1] = Reema and argv[2] =
Thareja
1.
Write a program to read a file character by character, and display it
simultaneously on the screen.
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
int ch;
char filename [20];
printf("\n Enter the filename:
");
fp = fopen(filename,
"r");
if (fp==NULL)
{
printf("\n Error Opening the
File");
exit (1);
}
ch= fgetc (fp);
while (ch!= EOF)
{
putchar (ch);
ch = fgetc (fp);
}
fclose(fp);
}
Output
Enter the filename: Letter.TXT
Hello how are you?
2.
Write a program to count the number of characters and number of lines in a
file.
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
int ch, no_of_characters= 0,
no_of_lines = 1;
char filename [20];
printf("\n Enter the filename:
");
fp = fopen(filename,
"r");
if (fp==NULL)
{
printf("\n Error Opening the
File");
exit(1);
}
ch= fgetc (fp);
while (ch!= EOF)
{
if (ch=='\n')
no_of_lines++;
no_of_characters++;
ch = fgetc (fp);
}
if (no_of_characters > 0)
printf("\n In the file %s,
there are %d lines and %d characters", filename, no_of_lines,
no_of_characters);
else
printf("\n File is
empty");
fclose(fp);
}
Output
Enter the filename: Letter.TXT
In the file Letter.TXT, there is 1
line and 18 characters
3.
Write a program to print the text of a file on screen by printing the text line
by line and displaying the line numbers before the text in each line. Use command
line argument to enter the filename.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp1;
char text [100], ch;
int line = 1;
int i=0;
clrscr();
if (argc != 2)
{
printf("\n Full information is
not provided. Please provide a filename");
return 0;
}
fpl = fopen (argv[1],
"r");
if (fpl == NULL)
{
printf("\n File Opening
Error");
return 0;
}
i = 0;
while (feof (fp1)==0)
{
fscanf (fp1, "%c",
&ch);
if (ch == '\n')
{
line++;
text [i]='\0';
printf("%d %s", line,
text);
i=0;
}
text [i] = ch;
i++;
}
text [i] = '\0';
printf("%s", text);
getch();
return 0;
}
Output
1 Hello how are you?
4.
Write a program to compare two files to check whether they are identical or
not.
#include <stdio.h>
#include <conio.h>
main()
{
FILE *fp1, *fp2;
int ch1, ch2;
char filenamel [20], filename2
[20];
clrscr();
printf("\n Enter the name of
the first file: ");
gets (filename1);
fflush(stdin);
printf("\n Enter the name of
the second file: ");
gets (filename2);
fflush(stdin);
if ((fp1= fopen(filenamel,
"r"))==0)
{
printf("\n Error opening the
first file");
exit(1);
}
if ((fp2=fopen (filename2,
"r"))==0)
{
printf("\n Error opening the
second file");
exit(1);
}
ch1= fgetc (fpl);
ch2= fgetc (fp2);
while (ch1!= EOF && ch2 !=
EOF && ch1==ch2)
{
/* Reading and comparing the
contents of two files */
ch1 = fgetc (fp1);
ch2 = fgetc (fp2);
}
if (ch1==ch2)
printf("\n Files are
identical");
else
printf("\n Files are not
identical");
fclose (fp1);
fclose (fp2);
getch();
return 0;
}
Output
Enter the name of the first
filename:
Comments.TXT
Enter the name of the second
filename:
Letter.TXT
Files are not identical
5.
Write a program to copy one file into another. Copy one character at a time.
#include <stdio.h>
#include <conio.h>
main()
{
FILE *fpl, *fp2;
int ch;
char filename1 [20], filename2
[20];
clrscr();
printf("\n Enter the name of
the first file: ");
gets (filename1);
fflush(stdin);
printf("\n Enter the name of
the second file: ");
gets (filename2);
fflush(stdin);
if ((fp1=fopen(filename1,
"r"))==0)
{
printf("\n Error opening the
first file");
exit (1);
}
if ((fp2= fopen(filename2,
"w"))==0)
{
printf("\n Error opening the
second file");
exit (1);
// Copy from fpl to fp2
ch = fgetc (fp1);
while (ch!= EOF)
{
putc(ch, fp2);
ch = fgetc (fp1);
}
printf("\n FILE COPIED");
fclose(fpl);
fclose(fp2);
getch();
return 0;
}
Output
Enter the name of the first
filename:
Comments.TXT
Enter the name of the second
filename:
User_Comments.TXT
FILE COPIED
6.
Write a program to copy one file into another. Copy multiple characters
simultaneously.
#include <stdio.h>
#include <conio.h>
main ()
{
FILE *fp1, *fp2;
char filenamel [20], filename2
[20];
clrscr();
printf("\n Enter the name of
the first filename: ");
gets (filename1);
fflush(stdin);
printf("\n Enter the name of
the second filename: ");
gets (filename2);
fflush(stdin);
if ((fp1=fopen(filename1,
"r"))==0)
{
printf("\n Error opening the
first file");
exit (1);
}
if ((fp2= fopen(filename2,
"w"))==0)
{
printf("\n Error opening the
second file");
exit (1);
}
while ((fgets (str,
sizeof(str),fp1)) != NULL) fputs (str, fp2);
fclose (fp1);
fclose (fp2);
getch();
return 0;
}
Output
Enter the name of the first
filename:
Comments.TXT
Enter the name of the second
filename:
User_Comments.TXT
FILE COPIED
7.
Write a program to read a file that contains characters. Encrypt the data in
this file while writing it into another file. (e.g., while writing the data in
another file you can use the formula ch = ch-2, i.e., if the data contains
character 'red' the encrypted data becomes 'pcb').
#include <stdio.h>
#include <conio.h>
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
char ch,
clrscr();
if (argc != 3)
{
printf("\n Full information is
not provided");
return 0;
}
fpl = fopen (argv[1],
"r");
if (fp1== NULL)
{
printf("\n File Opening
Error");
return 0;
}
fp2= fopen (argv[2],
"w");
if (fp2 == NULL)
{
printf("\n File Opening
Error");
return 0;
}
while (feof (fp1)==0)
{
fscanf (fpl, "%c",
&ch);
fprintf (fp2, "%c", ch -
2);
/*encrypted character is printed on
the screen */
}
printf("\n The encrypted data
is written to the file");
fclose (fp2);
fcloseall();
getch();
return 0;
}
Output
The encrypted data is written to
the file
8.
Write a program to read a file that contains lower case characters. Then write
these characters into another file with all lower case characters converted
into or upper case (e.g., if the file contains data - 'red' it must be written
in another file as 'RED').
#include <stdio.h>
#include <conio.h>
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
char ch;
clrscr();
if (argc != 3)
{
printf("\n Full information is
not provided");
return 0;
}
fpl = fopen (argv[1],
"r");
if (fpl == NULL)
{
printf("\n File Opening
Error");
return 0;
}
fp2= fopen (argv[2],
"r");
if (fp2 == NULL)
{
printf("\n File Opening
Error");
return 0;
}
while (feof (fp1) == 0)
{
fscanf(fp1, "%c",
&ch);
fprintf (fp2, "%c", ch -
32);
}
fcloseall();
printf("\n File copied with
upper case characters");
getch();
return 0;
}
Output
File copied with upper case
characters
9.
Write a program to merge two files into a third file. The names of the files
must be entered using command line arguments.
#include <stdio.h>
#include <conio.h>
int main(int argc, char *argv[])
{
FILE *fp1, *fp2, *fp3;
char ch;
clrscr();
if (argc != 4) // Read three
filenames from the user
{
printf("\n Full information is
not provided");
return 0;
}
fpl = fopen (argv[1],
"r");
if (fp1== NULL)
{
printf("\n File Opening
Error");
return 0;
}
Fp2= fopen (argv[2],
"w");
if (fp2== NULL)
{
printf("\n File Opening
Error");
return 0;
}
fp3= fopen (argv[3],
"w");
if (fp3== NULL)
{
printf("\n File Opening
Error");
return 0;
}
while (feof (fpl) != 0)
{
fscanf (fp1, "%c",
&ch);
fprintf (fp3, "%c", ch);
}
while (feof (fp2) != 0)
{
fscanf (fp2, "%c",
&ch);
fprintf (fp3, "%c", ch);
}
fcloseall();
printf("\n File merged");
getch();
return 0;
}
Output
File merged
10.
Write a program to read some text from the keyboard and store it in a file.
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
char filename [20], str[100];
printf("\n Enter the filename:
");
fp = fopen(filename, "w");
if (fp==NULL)
{
printf("\n Error Opening The
File");
exit(1);
}
printf("\n Enter the text:
");
gets (str);
fflush(stdin);
fprintf (fp, "%s", str);
fclose(fp);
}
Output
Enter the filename: Greet.TXT
Enter the text: Good Morning
11.
Write a program to read the details of a student and then print it on the
screen as well as write it into a file.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
typedef struct student
{
int roll_no;
char name [80];
float fees;
char DOB [80];
} STUDENT;
STUDENT studl;
clrscr();
fp =
fopen("student_details.dat", "w");
if (fp == NULL)
{
printf("\n File Opening
Error");
return 0;
}
printf("\n Enter the roll
number: ");
scanf("%d",
&studl.roll_no);
printf("\n Enter the name:
");
scanf("%s", studl.name);
printf("\n Enter the fees:
");
scanf("%f", &studl.
fees);
printf("\n Enter the DOB:
");
scanf("%s", stud1. DOB);
// PRINT ON SCREEN
printf("\n *** STUDENT'S
DETAILS *** " ) ;
printf("\n ROLL No. =
%d", studl.roll_no);
printf("\n NAME = %s",
studl.name);
printf("\n FEES = %f",
studl.fees);
printf("\n_DOB = %s",
stud1.DOB);
// WRITE TO FILE
fprintf (fp, "%d %s %f
%s", studl.roll_no, studl.name, stud1. fees, stud1. DOB);
fclose(fp);
getch();
return 0;
}
Output
Enter the roll number: 01
bu Enter the name: Aman
Enter the fees: 45000
Enter the DOB: 20-9-91
*****STUDENT'S DETAILS *******
ROLL NO. = 01
NAME = Aman
FEES = 45000
DOB = 20-9-91
12.
Write a program to read the details of a student from a file and then print it
on the screen.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
typedef struct student
{
int roll no;
char name [80];
float fees;
char DOB [80];
} STUDENT;
STUDENT studl;
clrscr();
fp =
fopen("student_details.dat", "w");
if (fp== NULL)
{
printf("\n File Opening
Error");
return 0;
}
// READ FROM FILE
fscanf (fp, "%d %s %f
%s", &studl.roll_no, tton studl.name, &studl.fees, stud1.DOB);
// PRINT ON SCREEN
printf("\n *** STUDENT'S
DETAILS ***");
printf("\n ROLL No. =
%d", studl.roll_no);
printf("\n NAME = %s",
studl.name);
printf("\n FEES = %f",
studl.fees);
printf("\n DOB = %s",
stud1. DOB);
fclose(fp);
getch();
return 0;
}
Output
*****STUDENT'S DETAILS *******
ROLL NO. = = 01
NAME = Aman
FEES = 45000
DOB = 20-9-91
13.
Write a program to read the details of student until a '-1' is entered and
simultaneously write the data to a file.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
typedef struct student
{
int roll_no;
char name [80];
float fees;
char DOB [80];
} STUDENT;
STUDENT studl;
clrscr();
fp =
fopen("student_details.dat", "r");
if (fp== NULL)
{
printf("\n File Opening
Error");
return 0;
}
printf("\n Enter the roll
number: ");
scanf("%d",
&studl.roll_no);
while (studl.roll_no != -1)2:
{
printf("\n Enter the name:
");
scanf("%s", stud1.name);
printf("\n Enter the fees:
");
scanf("%f",
&stud1.fees);
printf("\n Enter the DOB:
");
scanf("%s", stud1. DOB);
fprintf (fp, "%d %s %f
%s", studl.roll_no, studl.name, stud1. fees, stud1. DOB);
fflush(stdin);
printf("\n Enter the roll
number: ");
scanf("%d",
&studl.roll_no);
}
fclose(fp);
getch();
return 0;
}
Output
Enter the roll number: 01
Enter the name: Aman
Enter the fees: 45000
Enter the DOB: 20-9-91
Enter the roll number: 02
Enter the name: Divij
Enter the fees: 45000
Enter the DOB: 29-10-91
Enter the roll number: 03
Enter the name: Saransh
Enter the fees: 45000
Enter the DOB: 2-3-92
Enter the roll number: -1
14.
Write program to read characters until a ‘*’ is entered. Simultaneously store
these characters in a file.
#include <stdio.h>
#include <conio.h>
int main()
{
file *fp;
char ch;
clrscr();
fp =
fopen("characters.dat", "w");
if (fp == NULL)
{
printf("\n File Opening
Error");
return 0;
}
printf("\n Enter the
characters: ");
scanf("%c", &ch);
while (ch != '*')
{
fprintf (fp, "%c", ch);
scanf("%c", &ch);
}
printf("\n Written to the
file");
fclose (fp);
getch();
return 0;
}
Output
Enter the characters: abcdef*
Written to the file
15.
Write a program to count the number of lower case, upper case, numbers, and
special characters present in the contents of a file. (Assume that the file
contains the following data: 1. Hello, How are you?)
#include <stdio.h>
#include <conio.h>
int main(int arg c, char *argv[])
{
FILE *fp;
int ch, upper_case = 0, lower_case
= 0.
numbers = 0, special_chars = 0;
clrscr();
if (argc != 2)
{
printf("\n Full information is
not provided");
return 0;
}
fp = fopen(argv[1], "r");
if (fp == NULL)
{
printf("\n File Opening
Error");
return 0;
}
i=0;
while (feof (fp) == 0)
{
fscanf (fp, "%c",
&ch);
if (ch >= 'A' && ch
<= 'Z')
upper_ case++;
if (ch >= 'a' && ch
<= 'z')
lower_case++;
if (ch >= '0' && ch
<= '9')
numbers++;
else
special_chars++;
}
fclose (fp);
printf("\n Number of upper
case characters = %d", upper_case);
printf("\n Number of lower
case characters = %d", lower_case);
:\" printf("\n Number of
digits = %d", numbers);
printf("\n Number of special
characters = %d", special_chars);
getch();
return 0;
}
Output
Number of upper case characters = 2
Number of lower case characters = 2
Number of digits = 1
Number of special characters = 2
16.
Write a program to write record of students to a file using array of
structures.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
typedef struct student
{
int roll_no;
char name [80];
int marks;
} STUDENT;
STUDENT stud1 [5];
int i;
clrscr();
fp =
fopen("student_details.txt", "w");
if(fp == NULL)
{
printf("\n File Opening
Error");
return 0;
}
for (i= 0; i < 5;i++)
{
printf("\n Enter the roll
number: ");
scanf("%d", &stud1
[i].roll_no);
printf("\n Enter the name:
");
scanf("%s", studl
[i].name);
printf("\n Enter the marks:
");
scanf("%d", &stud1
[i].marks);
}
// PRINT ON SCREEN
for (i = 0; i < 5;i++)
printf("\n *** STUDENT'S
DETAILS ***"' );
printf("\n ROLL NO. =
%d", studl [i].roll_no);
eej printf("\n NAME =
%s", stud1 [i].name);
printf("\n MARKS = %d",
studi [i].marks);
// WRITE TO FILE
fprintf (fp, "%d %s %d",
studl [i].roll_no, studl [i].name, stud1 [i].marks);
}
printf("\n Data Written to the
file");
fclose(fp);
getch();
return 0;
}
Output
********STUDENT'S DETAILS ******
ROLL NO. = 01
NAME = Aditya
MARKS = 78
********STUDENT'S DETAILS *******
ROLL No. = 02
NAME = Goransh
MARKS = 100
********STUDENT'S DETAILS *******
ROLL No. = 03
NAME = Sarthak
MARKS = 81
Data Written to the file
17.
Write a program to append a record to the student's file.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
typedef struct student
{
int roll_no;
char name [80];
int marks;
} STUDENT;
STUDENT studl;
clrscr();
fp =
fopen("student_details.txt", "a");
if (fp == NULL)
{
printf("\n File Opening
Error");
return 0;
}
printf("\n Enter the Roll
Number =");
scanf("%d",
&stud1.roll_no);
printf("\n Enter the Name
=");
scanf("%s", stud1.name);
printf("\n Enter the Marks
=");
scanf("%d",
&stud1.marks);
fprintf("fp, "\n %d %s
%d", stud1.roll_no, studl.name, studl.marks);
/* After entering the record add a
-1 to the file to denote the end of records *
fprintf("fp, %d", -1);
printf("\n Data
Appended");
fclose(fp);
getch();
return 0;
}
Output
Enter the Roll Number =04
Enter the Name = Sanchita
Enter the Marks = 50
Data Appended
18.
Write a program to read the record of a particular student.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fpl;
typedef struct student
{
int roll_no;
char name [80];
int marks;
} STUDENT;
STUDENT stud1;
int found =0, rno;
clrscr();
fpl
fopen("student_details.txt", "r");
if (fpl == NULL)
{
printf("\n File Opening
Error");
return 0;
}
printf("\n Enter the roll
number of the student whose record has to be read: ");
scanf("%d", &rno);
while (1)
{
fscanf(fp1, "%d %s %d",
&studl.roll_no, studl.name, &stud1. marks);
if (studl.roll_no == -1)
break;
if (studl.roll_no== rno)
{
found = 1;
printf("\n The details of
student are");
printf("%d %s %d",
studl.roll_no,
stud1.name,stud1.marks);
break;
}
}
if (found==0)
printf("\n Record not found in
the file");
fclose (fpl);
return 0;
}
Output
Enter the roll number of the
student whose
record has to be read: 02
The details of student are 02
Goransh 100
19.
Write a program to edit the record of a particular student.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fpl, *fp2;
typedef struct student
{
int roll_no;
char name [80];
int marks;
} STUDENT;
STUDENT stud1;
int found =0, rno;
clrscr();
fpl =
fopen("student_details.txt", "r");
if (fpl == NULL)
{
printf("\n File Opening
Error");
return 0;
}
fp2= fopen (argv[2],
"w");
if (fp2 == NULL)
{
printf("\n File Opening
Error");
return 0;
}
printf("\n Enter the roll
number of the student whose record has to be modified: ");
scanf("%d", &rno);
while (1)
{
fscanf (fp1, "%d",
&studl.roll_no);
if (studl.roll_no == -1)
break;
if (studl.roll_no== rno)
{
found = 1;
fscanf (fpl, "%s %d",
studl.name, &studl.marks);
printf("\n The details of
existing record are ");
printf("%d %s %d",
studl.roll_no, stud1.name,stud1.marks);
printf("\n Enter the modified
name of the student: ");
scanf("%s", studl.name);
printf("\n Enter the modified
marks of the student: ");
scanf("%d",
&studl.marks);
/* Write the modified record to the
temporary file */
fprintf (fp2, "%d %s %d",
studl.roll_no, studl.name, studl.marks);
}
else
{
/* Copy the non-matching records to
the temporary file */
fscanf (fpl, "%s %d",
studl.name, &studl.marks);
fprintf (fp2, "%d %s %d",
studl.roll_no, studl.name, studl.marks);
}
}
fprintf (fp2, "%d", -1);
(fclose (fp1);
fclose(fp2);
if (found==0)
printf("\n The record with
roll number &d was not found in the file", rno);
else
{
fpl
fopen("student_details.txt", "w");
if (fpl == NULL)
{
printf("\n File Opening
Error");
return 0;
}
fp2 fopen("temp.txt",
"r");
if (fp2==NULL)
{
printf("\n File Opening
Error");
return 0;
}
/* Copy the contents of temporary
file into actual file */
fscanf (fp2, "%d",
&studl.roll_no);
if (studl.roll_no==-1)
break;
fscanf (fp2, "%s %d",
stud1. name, &studl.marks);
fprintf(fpl, "%d %s %d",
studl.roll_no, studl.name, stud1. marks);
}
}
fclose(fpl);
fclose(fp2);
printf("\n Record
Updated");
getch();
return 0;
}
Output
Enter the roll number of the
student whose record has to be modified: 03
The details of exisitng record are
-03 Sarthak 81
Enter the modified name of the
student : Sarthak
Enter the modified marks of the student:
85 Record Updated
20.
Write a program to delete the record of a particular student.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fpl, *fp2;
typedef struct student
{
int roll_no;
char name [80];
int marks;
} STUDENT;
STUDENT studl;
int found =0, rno;
clrscr();
fpl =
fopen("student_details.txt", "r");
if (fpl == NULL)
{
printf("\n File Opening
Error");
return 0;
}
fp2 = fopen("temp.txt",
"w");
if (fp2==NULL)
printf("\n File Opening
Error");
return 0;
printf("\n Enter the roll
number of the student whose record has to be deleted: ");
scanf("%d", &rno);
while (1)
{
fscanf (fpl, "%d",
&studl.roll_no);
if (studl.roll_no == -1)
break;
if (studl.roll_no == rno)
{
found = 1;
fscanf (fp1, "%s %d",
studl.name, &studl.marks);
}
// The matching record is not
copied to temp file
else
{
// Copy the non-matching records to
the temporary file
fscanf (fpl, "%s %d",
studl.name, &studl.marks);
fprintf (fp2, "%d %s %d
", studl.roll_no, studl.name, studl.marks);
}
}
fprintf (fp2, "%d", -1);
fclose(fpl);
fclose(fp2);
if (found==0)
printf("\n The record with
roll number &d was not found in the file", rno);
else
{
fpl
fopen("student_details.txt", "w");
if (fpl == NULL)
{
printf("\n File Opening
Error");
return 0;
}
fp2 = fopen("temp.txt",
"r");
if (fp2 == NULL)
{
printf("\n File Opening
Error");
return 0;
}
// Copy the contents of temporary
file into actual file
while (1)
{
fscanf (fp2, "%d",
&studl.roll_no);
if (studl.roll_no ==-1)
break;
fscanf (fp2, "%s %d",
studl.name, &studl.marks);
fprintf (fp1, "%d %s %d
", studl.roll_ no, studl.name, studl.marks);
}
}
fprintf (fpl, "%d ", -1);
fclose (fp1);
fclose (fp2);
printf("\n Record
Deleted");
/* The programmer may delete the
temp file which will no longer be required */ getch();
return 0;
}
Output
Enter the roll number of the
student whose record has to be deleted: 01
Record Deleted
21.
Write a program to store records of an employee in employee file. The data must
be stored using binary file.
#include <stdio.h>
#include <conio.h>
main()
{
typedef struct employee
{
int emp_code;
char name [20];
int hra;
int da;
int ta;
};
FILE *fp;
struct employee e [5];
int i;
fp =
fopen("employee.txt", "wb");
if (fp==NULL)
{
printf("\n Error opening
file");
exit (1);
}
printf("\n Enter the details
");
for (i = 0; i < 5; i++)
{
printf("\n\n Enter the
employee code:");
scanf("%d", &e
[i].emp_code);
printf("\n\n Enter the name of
the employee: ");
scanf("%s", e[i].name);
printf("\n\n Enter the HRA,
DA, and TA: ");
scanf("%d %d %d", &e
[i].hra, &e [i].da, &e [i].ta);
fwrite(&e [i], sizeof (e[i]),
1, fp);
}
fclose(fp);
getch();
return 0;
}
Output
Enter the employee code: 01
Enter the name of the employee:
Gargi
Enter the HRA, DA and TA: 10000
2000 5000
Enter the employee code: 02
Enter the name of the employee:
Nikita
Enter the HRA, DA and TA: 10000
2000 5000
22.
Write a program to read the records stored in 'employee.txt' file in binary
mode.
#include <stdio.h>
#include <conio.h>
main()
{
typedef struct employee
{
int emp_code;
char name [20];
int hra;
int da;
int ta;
};
FILE *fp;
struct employee e;
int i;
clrscr();
fp =
fopen("employee.txt", "rb");
if (fp==NULL)
{
printf("\n Error opening
file");
exit (1);
printf("\n THE DETAILS OF THE
EMPLOYEES ARE ");
while (1)
{
fread (&e, sizeof (e), 1, fp);
if (feof (fp))
break;
printf("\n\n Employee Code:
%d", e.emp_code);
printf("\n\n Name: %s",
e.name);
printf("\n\n HRA, DA, and TA:
%d %d %d", e.hra, e.da, e.ta);
}
fclose(fp);
getch();
return 0;
Output
Employee Code: 01
Name: Gargi
HRA, DA and TA: 10000 5000 2000
Employee Code: 02
Name: Nikita
HRA, DA and TA: 10000 5000 2000
23.
Write a program to append a record in the employee file (binary file).
#include <stdio.h>
#include <conio.h>
main()
{
typedef struct employee
{
int emp_code;
char name [20];
int hra;
int da;
int ta;
};
FILE *fp;
struct employee e;
int i;
fp =
fopen("employee.txt", "ab");
if (fp==NULL)
{
printf("\n Error opening
file");
exit(1);
}
printf("\n\n Enter the
employee code: ");
scanf("%d",
&e.emp_code);
printf("\n\n Enter the name of
employee: ");
scanf("%s", e.name);
printf("\n\n Enter the HRA,
DA, and TA: ");
scanf("%d %d %d",
&e.hra, &e.da, &e.ta);
fwrite(&e, sizeof (e), 1, fp);
fclose (fp);
printf("\n Record
Appended");
getch();
return 0;
}
Output
Enter the employee code: 06
Enter the name of employee: Tanya
Enter the HRA, DA and TA: 20000
10000 3000
24.
Write a program to edit the employee record stored in a binary file.
#include <stdio.h>
#include <conio.h>
main ()
{
typedef struct employee
{
int emp_code;
char name [20];
int hra;
int da;
int ta;
};
FILE *fp1, *fp2;
struct employee e;
slo abint i, ec, found = 0;
clrscr();
fp1 =
fopen("employee.txt", "rb");
if (fpl==NULL)
{
printf("\n Error opening
file");
exit(1);
}
fp2 =
fopen("temp_emp.txt", "wb");
if (fp2==NULL)
{
printf("\n Error opening
file");
exit (1);
}
printf("\n Enter the code of
the employee whose information has to be edited: ");
scanf("%d", &ec);
while (1)
{
fread (&e, sizeof (e),1,fpl);
if (feof (fp1))
break;
if (e.emp_code==ec)
{
found=1;
printf("\n The existing record
is: %d %s %d %d %d", e.emp_code, e.name, e.hra, e.ta, e.da);
printf("\n Enter the modified
name: ");
scanf("%s", e.name);
printf("\n Enter the modified
HRA, TA, and DA: ");
scanf("%d %d %d",
&e.hra, &e.ta, &e.da);
fwrite(&e, sizeof (e), 1, fp2);
}
else
fwrite(&e, sizeof (e),1,fp2);
}
fclose(fpl);
fclose(fp2);
if (found==0)
printf("\n Record not
found");
else
{
fpl =
fopen("employee.txt", "wb");
if (fp1==NULL)
{
printf("\n Error opening
file");
exit(1);
}
fp2 =
fopen("temp_emp.txt", "rb");
if (fp2==NULL)
{
printf("\n Error opening
file");
exit(1);
}
while (1)
{
fread (&e, sizeof (e), 1, fp2);
if (feof (fp2))
break;
fwrite(&e, sizeof (e), 1, fpl);
}
}
fclose (fpl);
fclose(fp2);
printf("\n Record
Edited");
getch();
return 0;
}
Output
Enter the code of the employee
whose information has to be edited: 01
The existing record is: 01 Gargi
10000 5000 2000
Enter the modified name: Gargi
Enter the modified HRA, TA, and DA:
20000 10000 30000
Record Edited
Programming in C: Unit V: File processing : Tag: : Syntax with Example C Programs | File processing - Accepting Command Line Arguments
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation