STRUCTURE
Structure is a
collection of data items of different data types using a single name.
Syntax
struct structure
name
{
data-type1
member-data1;
data-type2
member-data2;
};
Example
#include<stdio.h>
#include<conio.h>
struct marklist
{
int t,e,m,tot;
float avg;
char name[20];
}s;
void main()
{
clrscr();
printf("Enter the name:");
scanf("%s",s.name);
printf("Enter 3 marks:");
scanf("%d%d%d",&s.t,&s.e,&s.m);
s.tot=s.t+s.e+s.m;
s.avg=s.tot/3.0;
printf("Total=%d"
,s.tot);
printf("\nAverage=%f",s.avg);
getch();
}
Arrays
of structure
#include<stdio.h>
#include<conio.h>
struct add
{
int t,e,m,tot;
float avg;
char name[20];
}
s[10];
void main()
{
int i;
clrscr();
for(i=0;i<3;i++)
{
printf("\nEnter the name:");
scanf("%s",s[i].name);
printf("Enter 3 marks:");
scanf("%d%d%d",&s[i].t,&s[i].e,&s[i].m);
s[i].tot=s[i].t+s[i].e+s[i].m;
s[i].avg=s[i].tot/3.0;
printf("Total=%d
",s[i].tot);
printf("\nAverage=%f",s[i].avg);
}
getch();
}
FILES
Collection of
records (data) is called as a file. A file is a place on the disk where a group
of related data is stored.
Basic file
operations:
Naming a file
Opening a file
Writing data into
a file
Reading data from
a file
Closing a file
File functions
fopen() – create a
new file and opens an existing file
fclose() – Close a
file
getc()- Reads a
character from a file
putc()-Writes a
character from a file
getw()-Reads
integer from a file
put()-Writes
integer to a file
fscanf()-Reads the
set of data from a file
fprintf()-Writes a
set of data from a file
fseek()-Sets the
position to a desired point in the file
Opening a file
Syntax
FILE *
filepointer;
Filepointer
=fopen(“filename”,”mode”);
Mode of file
“r” – Read only
(read mode)
“w” – Write
only(write mode)
“a” – Append
only(read & write)
Close a file
Syntax
fclose(filepointer);
fprintf()
syntax
fprintf(filepointer,”control
string”,variable1,variable2,…..);
fscanf
syntax
fscanf(filepointer,”control
string”,&variable1,variable2,…)
Example
#include<conio.h>
#include<stdlib.h>
main()
{
char ch;
FILE *fp;
clrscr();
printf("Data
Input");
fp=fopen("INPUT","w");
while((ch=getchar())!=EOF)
putc(ch,fp);
fclose(fp);
fp=fopen("INPUT","r");
printf("Data
Output");
while((ch=getc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
getch();
}
Write the program
to read the character from the keyboard and write on to a file
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
char ch;
FILE *fp;
clrscr();
fp=fopen("bio.txt","w");
if(fp==NULL)
{
printf("File
is not opened");
exit(0);
}
scanf("%c",&ch);
while(ch!='$')
{
fputc(ch,fp);
scanf("%c",&ch);
}
fclose(fp);
getch();
}
Copy one file to
another
Save two files(a1.doc,a2.doc)
a1.doc=Type
anything
a2.doc=Empty
Then run the main
file.
a1.doc is copied
into a2.doc file.
Example
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
char ch;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("a1.doc","r");
fp2=fopen("a2.doc","w");
if(fp1==NULL)
{
printf("File
is not opened");
exit(0);
}
if(fp2==NULL)
{
printf("File
is not opened");
exit(0);
}
ch=fgetc(fp1);
while(ch!=EOF)
{
fputc(ch,fp2);
ch=getc(fp1);
}
fclose(fp1);
fclose(fp2);
getch();
}
Compare two files
Save two files (a1.doc,a2.doc)
a1.doc=Type
anything
a2.doc=Type the same
thing
Then run the main
file.
It will give the
True Result.
Example
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
char ch1,ch2;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("a1.doc","r");
fp2=fopen("a2.doc","r");
if(fp1==NULL)
{
printf("File
is not opened");
exit(0);
}
if(fp2==NULL)
{
printf("File
is not opened");
exit(0);
}
ch1=fgetc(fp1);
ch2=fgetc(fp2);
while(ch1!=EOF ||
ch2!=EOF)
{
if(ch1==ch2)
printf("T");
else
printf("F");
ch1=fgetc(fp1);
ch2=fgetc(fp2);
}
fclose(fp1);
fclose(fp2);
getch();
}

No comments:
Post a Comment