Professional Documents
Culture Documents
Unit Iv
Unit Iv
MODE DESCRIPTION
r Open text file in read mode
Read only • If file exits, the marker is positioned at beginning.
• If file doesn’t exist, error returned
w Open text file in write mode.
Write only • If file exits, the previous data is erased and positioned at beginning.
• If file doesn’t exist, it is created.
a+
Append and Read Opens text file for reading as well as appending modes.
❖The first argument is the stream pointer, is the pointer to the streams that has been declared and
associated with a text file.
int rno, m;
FILE *fp;
fp = fopen (“mydata.txt", "r”);
fscanf (fp, "%d %d", &rno, &m);
The only difference between scanf and fscanf is that scanf reads data from the stdin (input stream)
and fscanf reads input from a user specified stream(stdin or file).
❖Where stream_pointer is a file pointer associated with a file that has been opened for writing.
PROGRAM:
#include<stdio.h>
int main()
{
FILE *fp;
int num;
char name[20];
int m;
fp = fopen("student.txt","w");
printf("Enter RollNum,name,marks\n");
while((fscanf(stdin,"%d%s%d",&num,name,&m))!=EOF)
{
fprintf(fp,"%d%s%d\n",num,name,m);
}
fclose(fp);
return 0;
}
OUTPUT
Enter RollNum,name,marks
670 JOHN 76
870 KUMAR 67
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char filename[100];
char ch;
int linecount, wordcount, charcount;
linecount = 0;
wordcount = 0;
charcount = 0;
printf("Enter a filename :");
scanf(“%s”,filename);
fp = fopen(filename,"r");
if (fp== NULL)
{
puts("Could not open files");
exit(0);
}
if (charcount > 0)
{
printf("Lines : %d \n", ++ linecount);
printf("Words : %d \n", wordcount);
printf("Characters : %d \n", charcount);
}
return 0;
}
in.txt
Hello welcome
OUTPUT
File Operations
1. Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w+”)
2. Opening an existing file (fopen)
3. Reading from file (fscanf or fgets or fgetc)
4. Writing to a file (fprintf or fputs or fputc)
5. Moving to a specific location in a file (fseek, rewind)
6. Closing a file (fclose)
Program:
#include<stdio.h>
int main()
{
FILE *f1,*f2; // file pointer declaration
char ch;
f1 = fopen("in.txt","r"); // opening a file in read mode
if(f1==NULL) // if file not found report an error message
printf("File does not exist");
else
{
f2 = fopen("out.txt","w"); // opening a file in write mode
while((ch=fgetc(f1))!=EOF) // reading a character till EOF
{
fputc(ch,f2); // writing character into a file
}
}
fclose(f1);
fclose(f2);
return 0;
}
in.txt
Hello Welcome
OUTPUT
out.txt
Hello Welcome
Text file(ASCII file) – used to store human readable characters as a rich text document or plain
text document. Text files also store data in sequential bytes but bits in text file represents
characters.
Binary file-contains any type of data encoded in binary form for computer storage and
processing purpose.
#include<stdio.h>
int main()
{
FILE *f1,*f2;
char ch;
f1 = fopen("in.txt","r");
if(f1==NULL)
printf("File does not exist");
else
{
f2 = fopen("out.txt","w");
while((ch=fgetc(f1))!=EOF)
{
fputc(ch,f2);
}
}
fclose(f1);
fclose(f2);
return 0;
}
Hello Welcome
OUTPUT
out.txt
Hello Welcome
#include<stdio.h>
int main()
{
FILE *f1,*f2;
char ch;
f1 = fopen("in.txt","r");
if(f1==NULL)
printf("File does not exist");
else
{
f2 = fopen("out.txt","w");
while((ch=getc(f1))!=EOF)
{
putc(ch,f2);
}
}
fclose(f1);
fclose(f2);
return 0;
}
Hello Welcome
OUTPUT
out.txt
Hello Welcome
A structure is a collection of data items of different data types stored under a common name.
Syntax:
struct structure_name
{
datatype structure_element1;
datatype structure_element2;
…………….
…………….
datatype structure_elementn;
};
#include<stdio.h>
struct student
{
int no;
char name[10];
int m1,m2,m3;
}s;
void main()
{
float total,avg;
printf("rollno:");
scanf("%d",&s.no);
printf("name:");
scanf("%s",s.name);
printf("three marks");
scanf("%d%d%d",&s.m1,&s.m2,&s.m3);
total=s.m1+s.m2+s.m3;
avg=total/3;
printf("total=%f avg=%f", total,avg);
}
OUTPUT:
rollno: 1
name: raji
three marks 78 78 78
total= 234.000000
avg= 78.000000
9. Declare a structure for employee entity with suitable attributes? Initialize employee structure
variable. Write the statements to display all the details using pointer.
#include<stdio.h>
struct employee
{
char empname[10];
int empid;
float salary;
};
void main()
{
struct employee e,*emp;
emp=&e;
printf("enter empname, empid, salary\n");
scanf("%s%d%f",emp->empname,&emp->empid,&emp->salary);
printf("Employee name is %s \n",(*emp).empname);
printf("Employee id is %d \n",(*emp).empid);
printf("Employee salary is %f \n",(*emp).salary);
OUTPUT
enter empname, empid, salary
arun
1000
20000
Employee name is arun
Employee id is 1000
Employee salary is 20000.000000
10. With suitable examples compare structure and union. Create a union as part of a structure.
#include<stdio.h>
union doj
{
int dd;
int mon;
int year;
};
struct employee
{
char empname[10];
int empid;
float salary;
union doj dat;
};
void main()
{
struct employee emp;
printf("enter empname, empid, salary, date of joining\n");
scanf("%s%d%f%d%d
%d",emp.empname,&emp.empid,&emp.salary,&emp.dat.dd,&emp.dat.mon,&emp.dat.year);
printf("Employee name is %s \n",emp.empname);
printf("Employee id is %d \n",emp.empid);
printf("Employee salary is %f \n",emp.salary);
printf("Employee day is %d \n",emp.dat.dd);
printf("Employee month is %d \n",emp.dat.mon);
printf("Employee year is %d \n",emp.dat.year);
}
OUTPUT
11. Write a C program to print maximum marks in each subject along with the name of the
student by using structures. Take 3 subjects and 3students records.
#include<stdio.h>
struct stud
{
char name[10];
int id;
int m[3];
}s[3];
int main()
int i;
for(i=0;i<3;i++)
{
printf("Enter Name ID M1 M2 M3 of student:%d\n",i+1);
scanf("%s%d%d%d%d",s[i].name,&s[i].id,&s[i].m[0],&s[i].m[1],&s[i].m[2]);
}
printf("Name ID M1 M2 M3 of students are\n");
for(i=0;i<3;i++)
{
printf("%s\t%d\t%d\t%d\t%d\n",s[i].name,s[i].id,s[i].m[0],s[i].m[1],s[i].m[2]);
}
for(i=0;i<3;i++)
greaterMarks(i);
return 0;
}
void greaterMarks(int i)
{
printf("From sub:%d among 3 students\n",i+1);
if(s[0].m[i]>s[1].m[i]&&s[0].m[i]>s[2].m[i])
{
printf("%d is greater\n",s[0].m[i]);
}
else if(s[1].m[i]>s[0].m[i]&&s[1].m[i]>s[2].m[i])
{
printf("%d is greater\n",s[1].m[i]);
}
else if(s[2].m[i]>s[0].m[i]&&s[2].m[i]>s[1].m[i])
{
printf("%d is greater\n",s[2].m[i]);
}
}
Output
arun 1 67 78 65
ram 2 78 65 67
sriram 3 98 56 67
arun 1 67 78 65
ram 2 78 65 67
sriram 3 98 56 76