Grade XII C-Programming
Review of C-Programming
C programming language and its features
C is a versatile programming language
known for its simplicity and portability. It combines low-level and high-level
features, making it suitable for both system and application development. With
a focus on structured programming, C allows the creation and testing of modules
independently, promoting code organization. Its machine independence enables
programs to run on various systems. C is fast, reliable, and facilitates easy
program extension by adding new features.
- Simple and easy to learn.
- Machine-independent, allowing
programs to run on different systems.
- Combines features of both low-level
and high-level languages.
- Suitable for developing system and
application programs.
- Supports structural programming;
modules can be prepared and tested separately.
- Fast and reliable performance.
- Programs can be easily extended by adding new features and operations.
File Handling in C
In
C programming, file handling is essential for storing and retrieving data
permanently in secondary storage. Unlike variables in RAM, files allow data to
persist beyond program execution.
Opening
a Data File
FILE
*fptr;
fptr
= fopen("filename", "mode");
-
"w" : Write/store data in a file.
-
"r" : Read/retrieve data from a file.
-
"a" : Add/append data to an existing file.
Storing/
Writing Data
fprintf(fptr,
"format specifiers", variables);
Example:
fprintf(fptr,
"%s %s %d %d", name, disease, age, bedNumber);
Program
Examples
1)
Create a Datafile "patient.txt"
#include
<stdio.h>
int
main() {
char name[10], disease[10];
int age, bedNumber;
FILE *fptr;
fptr = fopen("patient.txt",
"w");
printf("Enter name, disease, age, and
bed number: ");
scanf("%s %s %d %d", name,
disease, &age, &bedNumber);
fprintf(fptr, "%s %s %d %d\n",
name, disease, age, bedNumber);
fclose(fptr);
return 0;
}
2)
Create a Datafile "student.txt" to store name and marks of 3 subject
for user desired student.
#include
<stdio.h>
int
main() {
char name[10];
int class, marks1, marks2, marks3, num, i;
FILE *fptr;
fptr = fopen("student.txt",
"w");
printf("How many records? ");
scanf("%d", &num);
for (i = 1; i <= num; i++) {
printf("Enter name, class, and 3
marks: ");
scanf("%s %d %d %d %d", name,
&class, &marks1, &marks2, &marks3);
fprintf(fptr, "%s %d %d %d
%d\n", name, class, marks1, marks2, marks3);
}
fclose(fptr);
return 0;
}
3)
Add/Append Data to "student.txt"
#include
<stdio.h>
int
main() {
char name[10], ch[3];
int class, marks1, marks2, marks3;
FILE *fptr;
fptr = fopen("student.txt",
"a");
if
(fptr == NULL) {
printf("Error opening the
file.\n");
return 1; // Exit the program with an error code
}
do {
printf("Enter name, class, and 3
marks: ");
scanf("%s %d %d %d %d", name,
&class, &marks1, &marks2, &marks3);
fprintf(fptr, "%s %d %d %d
%d\n", name, class, marks1, marks2, marks3);
printf("Press Y to continue:
");
scanf("%s", ch);
} while (ch[0] == 'Y' || ch[0] == 'y');
fclose(fptr);
return 0;
}
//Example
4 Read
#include
<stdio.h>
int
main() {
char name[10];
int class, marks1, marks2, marks3;
FILE *fptr;
fptr = fopen("student.txt",
"r");
if (fptr == NULL) {
printf("Error opening the
file.\n");
return 1; // Exit the program with an error code
}
printf("Name\tClass\tMarks1\tMarks2\tMarks3\n");
while (fscanf(fptr, "%s %d %d %d
%d", name, &class, &marks1, &marks2, &marks3) != EOF) {
printf("%s\t%d\t%d\t%d\t%d\n", name, class, marks1, marks2,
marks3);
}
fclose(fptr);
return 0;
}
//Example
5 read
#include <stdio.h>
int
main() {
char name[10];
int class, marks1, marks2, marks3;
FILE *fptr;
fptr = fopen("student.txt",
"r");
if (fptr == NULL) {
printf("Error opening the
file.\n");
return 1;
}
printf("Passing Students:\n");
printf("Name\tClass\tMarks1\tMarks2\tMarks3\n");
while (fscanf(fptr, "%s %d %d %d
%d", name, &class, &marks1, &marks2, &marks3) != EOF) {
if (marks1 >= 40 && marks2
>= 40 && marks3 >= 40) {
printf("%s\t%d\t%d\t%d\t%d\n", name, class, marks1, marks2,
marks3);
}
}
printf("\nFailing Students:\n");
printf("Name\tClass\tMarks1\tMarks2\tMarks3\n");
fclose(fptr);
return 0;
}
//Example
6 read
#include
<stdio.h>
int
main() {
char name[10];
int class, marks1, marks2, marks3;
FILE *fptr;
fptr = fopen("student.txt",
"r");
if (fptr == NULL) {
printf("Error opening the
file.\n");
return 1;
}
int passCount = 0;
int failCount = 0;
printf("Passing Students:\n");
printf("Name\tClass\tMarks1\tMarks2\tMarks3\n");
while (fscanf(fptr, "%s %d %d %d
%d", name, &class, &marks1, &marks2, &marks3) != EOF) {
if (marks1 >= 40 && marks2
>= 40 && marks3 >= 40) {
printf("%s\t%d\t%d\t%d\t%d\n", name, class, marks1, marks2,
marks3);
passCount++;
} else {
failCount++;
}
}
printf("\nNumber of Passing Students:
%d\n", passCount);
printf("Number of Failing Students: %d\n", failCount);
fclose(fptr);
return 0;
}
Comments
Post a Comment