Skip to content

Commit 7308d76

Browse files
committed
Working with structures and files
1 parent e5144a5 commit 7308d76

25 files changed

+1184
-0
lines changed

6_STRUCT/IT.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
struct IT
3+
{
4+
int roll;
5+
char name[20];
6+
float per;
7+
} s[10];
8+
void main()
9+
{
10+
11+
void input();
12+
void display();
13+
14+
printf("Enter Roll no, Name, Marks%:\n");
15+
input();
16+
printf("\n your data is:\n");
17+
display();
18+
}
19+
void input()
20+
{
21+
int i;
22+
for (i = 0; i <= 2; i++)
23+
{
24+
scanf("%d%s%f", &s[i].roll, s[i].name, &s[i].per);
25+
}
26+
}
27+
void display()
28+
{
29+
int i;
30+
31+
for (i = 0; i <= 2; i++)
32+
{
33+
printf("Roll=%d Name:%s per=%f\n", s[i].roll, s[i].name, s[i].per);
34+
printf("name");
35+
if (s[i].roll % 2 == 0)
36+
printf("Name=%s\n", s[i].name);
37+
}
38+
}

6_STRUCT/Rectangle_check.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//check if two rectangles overlap with each other
2+
3+
#include <stdio.h>
4+
typedef struct
5+
{
6+
int x, y;
7+
} point;
8+
9+
typedef struct
10+
{
11+
point l; //top left point
12+
point r; //bottom right point
13+
} rectangle;
14+
15+
typedef enum
16+
{
17+
false,
18+
true
19+
} boolean;
20+
21+
boolean Do_overlap(rectangle R1, rectangle R2)
22+
{
23+
if (R2.r.x < R1.l.x || R2.l.x > R1.r.x)
24+
return false;
25+
if (R1.l.y < R2.r.y || R2.l.y < R1.r.y)
26+
return false;
27+
return true;
28+
}
29+
void main()
30+
{
31+
rectangle R1, R2;
32+
33+
printf("Enter the co-ordinates for rectangle 1:");
34+
scanf("%d%d", &R1.l.x, &R1.l.y);
35+
scanf("%d%d", &R1.r.x, &R1.r.y);
36+
printf("Enter the co-ordinates for rectangle 2:");
37+
scanf("%d%d", &R2.l.x, &R2.l.y);
38+
scanf("%d%d", &R2.r.x, &R2.r.y);
39+
if (Do_overlap(R1, R2))
40+
printf("Rectangle overlaps");
41+
else
42+
printf("Rectangle does not overlaps");
43+
}

6_STRUCT/dates.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
2+
3+
struct date
4+
{
5+
int dd;
6+
int mm;
7+
int yy;
8+
} d1, d2;
9+
10+
int main()
11+
{
12+
printf("Input two dates(DD/MM/YY): ");
13+
scanf("%d%*c%d%*c%d", &d1.dd, &d1.mm, &d1.yy);
14+
scanf("%d%*c%d%*c%d", &d2.dd, &d2.mm, &d2.yy);
15+
if ((d1.dd == d2.dd) && (d1.mm == d2.mm) && (d1.yy == d2.yy))
16+
printf("dates are equal");
17+
else
18+
printf("dates not are equal");
19+
return 0;
20+
}

6_STRUCT/students.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
3+
struct students
4+
{
5+
int roll_no, year;
6+
char name[20], department[20], course[20];
7+
};
8+
9+
void main()
10+
{
11+
struct students s[10];
12+
int i, y;
13+
14+
for (i = 0; i < 10; i++)
15+
{
16+
printf("Enter Roll No, Name, Department,Course,Year of Joining of s[%d]:\n", i);
17+
scanf("%d%s%s%s%d", &s[i].roll_no, s[i].name, s[i].department, s[i].course, &s[i].year);
18+
}
19+
printf("Enter The Year to Check the Data:\n");
20+
scanf("%d", &y);
21+
printf("\n\nThe Students who joined in the year %d:\n", y);
22+
for (i = 0; i < 10; i++)
23+
{
24+
if (y == s[i].year)
25+
{
26+
printf("\nRoll No.= %d\tName =%s\tDepartment =%s\tCourse =%s", s[i].roll_no, s[i].name, s[i].department, s[i].course);
27+
}
28+
}
29+
}

7_FILE/CopyTheContentOfFile.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdio.h>
2+
3+
void main()
4+
{
5+
FILE *fs, *ft;
6+
char ch;
7+
fs = fopen("abc.txt", "r");
8+
if (fs == NULL)
9+
{
10+
printf("\nUnable to open source file......");
11+
return 0;
12+
exit(0);
13+
}
14+
ft = fopen("nnnm", "w");
15+
if (ft == NULL)
16+
{
17+
printf("\nUnable to open target file......");
18+
return 0;
19+
exit(0);
20+
}
21+
while (1)
22+
{
23+
ch = fgetc(fs);
24+
fputc(ch, ft);
25+
if (ch == EOF)
26+
break;
27+
}
28+
fclose(fs);
29+
fclose(ft);
30+
printf("File copied successfully.......");
31+
}

7_FILE/CountCharacters.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
3+
void main()
4+
{
5+
int noc, nob, nol;
6+
FILE *fp;
7+
char ch;
8+
fp = fopen("45.c", "r");
9+
if (fp == NULL)
10+
{
11+
printf("\nUnable to open source file......");
12+
return 0;
13+
exit(0);
14+
}
15+
16+
while (1)
17+
{
18+
ch = fgetc(fp);
19+
if (ch == EOF)
20+
break;
21+
if (ch == ' ')
22+
nob++;
23+
if (ch == '\n')
24+
nol++;
25+
noc++;
26+
}
27+
fclose(fp);
28+
printf("\nBlanks = %d\nLines = %d\nCharacters = %d", nob, nol, noc);
29+
}

7_FILE/FILE1.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// writes records to a file using structure
2+
#include <stdio.h>
3+
4+
#include <stdlib.h>
5+
6+
void main()
7+
{
8+
FILE *fp;
9+
char another = 'y';
10+
struct emp
11+
{
12+
char name[40];
13+
int age;
14+
float bs;
15+
};
16+
struct emp e;
17+
18+
fp = fopen("EMPLOYEE.DAT", "w");
19+
if (fp == NULL)
20+
{
21+
puts("Cannot Open File....");
22+
exit(1);
23+
}
24+
while (another == 'y')
25+
{
26+
printf("\nEnter name,age and basic salary:");
27+
scanf("%s %d %f", e.name, &e.age, &e.bs);
28+
fprintf(fp, "%s %d %f\n", e.name, e.age, e.bs);
29+
30+
printf("ADD Another record(y/n):");
31+
fflush(stdin);
32+
another = getche();
33+
}
34+
fclose(fp);
35+
}

7_FILE/FILE2.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// read records to a file using structure
2+
#include <stdio.h>
3+
4+
#include <stdlib.h>
5+
6+
void main()
7+
{
8+
FILE *fp;
9+
struct emp
10+
{
11+
char name[40];
12+
int age;
13+
float bs;
14+
};
15+
struct emp e;
16+
17+
fp = fopen("EMPLOYEE.DAT", "r");
18+
if (fp == NULL)
19+
{
20+
puts("Cannot Open File....");
21+
exit(1);
22+
}
23+
while (fscanf(fp, "%s %d %f", e.name, &e.age, &e.bs) != EOF)
24+
printf("\n%s %d %f", e.name, e.age, e.bs);
25+
26+
fclose(fp);
27+
}

7_FILE/FILE3.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// writes records to a file using structure in binary mode
2+
#include <stdio.h>
3+
4+
#include <stdlib.h>
5+
6+
void main()
7+
{
8+
FILE *fp;
9+
char another = 'y';
10+
struct emp
11+
{
12+
char name[40];
13+
int age;
14+
float bs;
15+
};
16+
struct emp e;
17+
18+
fp = fopen("EMPLOYEE.DAT", "wb");
19+
if (fp == NULL)
20+
{
21+
puts("Cannot Open File....");
22+
exit(1);
23+
}
24+
while (another == 'y')
25+
{
26+
printf("\nEnter name,age and basic salary:");
27+
scanf("%s %d %f", e.name, &e.age, &e.bs);
28+
fwrite(&e, sizeof(e), 1, fp);
29+
30+
printf("ADD Another record(y/n):");
31+
fflush(stdin);
32+
another = getche();
33+
}
34+
fclose(fp);
35+
}

7_FILE/FILE4.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// read records to a file using structure in binary mode
2+
#include <stdio.h>
3+
4+
#include <stdlib.h>
5+
6+
void main()
7+
{
8+
FILE *fp;
9+
struct emp
10+
{
11+
char name[40];
12+
int age;
13+
float bs;
14+
};
15+
struct emp e;
16+
17+
fp = fopen("EMPLOYEE.DAT", "r");
18+
if (fp == NULL)
19+
{
20+
puts("Cannot Open File....");
21+
exit(1);
22+
}
23+
while (fread(&e, sizeof(e), 1, fp) == 1)
24+
printf("\n%s %d %f", e.name, e.age, e.bs);
25+
26+
fclose(fp);
27+
}

7_FILE/FILEcopy.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//File copy by using argc and argv
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
void main(int argc, char *argv[])
7+
{
8+
FILE *ft, *fs;
9+
char ch;
10+
11+
if (argc != 3)
12+
{
13+
puts("improper number of arguments");
14+
exit(0);
15+
}
16+
fs = fopen(argv[1], "r");
17+
if (fs == NULL)
18+
{
19+
puts("Cannot open source file....");
20+
exit(0);
21+
}
22+
ft = fopen(argv[2], "w");
23+
if (ft == NULL)
24+
{
25+
puts("Cannot open target file....");
26+
fclose(fs);
27+
exit(0);
28+
}
29+
while (1)
30+
{
31+
ch = fgetc(fs);
32+
if (ch == EOF)
33+
break;
34+
else
35+
fputc(ch, ft);
36+
}
37+
fclose(fs);
38+
fclose(ft);
39+
}

0 commit comments

Comments
 (0)