0% found this document useful (0 votes)
45 views23 pages

Master Tutorials

The document discusses structures in C programming, including: 1) Defining a structure with declaration and initialization using an example. 2) Passing a structure variable as a parameter to a function. 3) Maintaining records of student details using an array of structures. It also explains common file handling functions in C like fopen(), fclose(), fprintf(), fscanf(), fseek() etc. with examples. Finally, it provides a program to create an integer data file and segregate even and odd numbers into separate files.

Uploaded by

anand_gudnavar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views23 pages

Master Tutorials

The document discusses structures in C programming, including: 1) Defining a structure with declaration and initialization using an example. 2) Passing a structure variable as a parameter to a function. 3) Maintaining records of student details using an array of structures. It also explains common file handling functions in C like fopen(), fclose(), fprintf(), fscanf(), fseek() etc. with examples. Finally, it provides a program to create an integer data file and segregate even and odd numbers into separate files.

Uploaded by

anand_gudnavar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

PCD MASTER

TUTORIAL
15/05/2018
1. Define structure? Explain its
declaration and initialization with an
example.
 A structure can be defined as a user
defined data type that stores group of
related data elements of different data
types with a common name
 Declaration:
INITIALIZATION
2. Show how structure variable
are passed as a parameter to a
function with example
#include <stdio.h>
#include <string.h>
struct student //structure definition
void func(struct student record)
{ {
int id; printf(" Id is: %d \n",
char name[20]; record.id);
float percentage;
printf(" Name is: %s \n",
};
record.name);
void func(struct student record); // function printf(" Percentage is: %f \n",
prototype record.percentage);
}
main()
{
struct student record;

record.id=1; Output:
strcpy(record.name, "Cooper"); Id is: 1
record.percentage = 86.5;
Name is: Cooper
func(record); Percentage is: 86.500000
return 0;
}
3. Write a C program to maintain
record of n students detail
using array of structures with
four fields(rno, name, marks,
grade). Assume appropriate
data type for each field. Print
marks of student given the
name of student.
#include<stdio.h>
#include<string.h>
#include<process.h>
struct student
{
int rollno;
int marks;
char name[20];
char grade;
};
 
void main()
{
int i,n;
struct student s[10];
char key[10];
printf("Enter the no of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter student %d
details\n",i+1);
printf("Enter the roll no:\n");
scanf("%d",&s[i].rollno);
printf("Enter name:\n");
scanf("%s",s[i].name);
printf("Enter marks:\n");
scanf("%d",&s[i].marks);
printf("Enter grade:\n");
scanf(" %c",&s[i].grade);
}
printf("\n\nStudent Details are:\n\n");
printf("\nRollno\tName\tMarks\tGrade\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\t
%d\n",s[i].rollno,s[i].name,s[i].marks,s[i].grade);
}
printf("Enter the Student name\n");
scanf("%s", key);
for(i=0;i<n;i++)
{
if(strcmp(s[i].name,key)==0)
{
printf("\n Marks=%d\n",s[i].marks);
exit(0);
}
}
printf("Entered student name not found\n");
}
4 .Explain the following
functions
fseek(),fprintf(),fscanf(),fop
en(),fclose(),fgets(),fputs()
Functions fprintf() and fscanf() are the
file version of printf() and fscanf(). The
only difference while using fprintf() and
fscanf() is that, the first argument is a
pointer to the structure FILE .
Writing to a file
#include <stdio.h>
void main()
{
int n;
FILE *fptr;
fptr=fopen("C:\\program.txt","w");
if(fptr==NULL)
{
printf("Error!");
exit(1);
} printf("Enter n: ");
scanf("%d",&n);
fprintf(fptr,"%d",n);
fclose(fptr);
}
Reading from file
#include <stdio.h>
int main()
{
int n;
FILE *fptr;
if ((fptr=fopen("C:\\program.txt","r"))==NULL)
{
printf("Error! opening file");
exit(1); /* Program exits if file pointer returns NULL. */ }
fscanf(fptr,"%d",&n);
printf("Value of n=%d",n);
fclose(fptr);
return 0;
}
Opening a file is performed using library function fopen(). The
syntax for opening a file in standard I/O is:
ptr=fopen("fileopen","mode")
 r Open for reading. If the file does not exist, fopen()
returns NULL.
 w Open for writing. If the file exists, its contents are
overwritten. If the file does not exist, it will be created.
 a Open for append. i.e, Data is added to end of file. If the
file does not exists, it will be created.
 r+ Open for both reading and writing. If the file does not
exist, fopen() returns NULL.
 w+ Open for both reading and writing. If the file exists, its
contents are overwritten. If the file does not exist, it will be
created.
 a+ Open for both reading and appending. If the file
does not exists, it will be created.
The fseek() function is most useful in
random access files where either the record
(or block) size is known, or there is an
allocation system that denotes the start and
end positions of records in an index portion
of the file. The fseek() function takes three
parameters
FILE * f – the file pointer;
long offset – the position offset;
 int origin – the point from which the offset
is applied.
(SEEK_SET,SEEK_CUR,SEEK_END )
fseek( f, 0, SEEK_SET);
 The fputs() function is used to write string(array of
characters) to the file.
fputs(char str[], FILE *fp);

 The fputs() function takes two arguments, first is the


string to be written to the file and second is the file
pointer where the string will be written.
 The fgets() function is used to read string(array of
characters) from the file.
fgets(char str[],int n, FILE *fp);

 The fgets() function takes three arguments, first is the


string read from the file, second is size of string(character
array) and third is the file pointer from where the string
will be read. 
The fgets() function will return NULL value when it reads
EOF(end-of-file).
5.Write a c program to create a integer
data file and then segregate even and
odd integers into two separate files.
#include <stdio.h>
#include<stdlib.h>
main()
{
int n=0,i,n1;

FILE *fptr,*fptr_e,*fptr_o;

fptr=fopen("D:/all_num.txt","w");
fptr_e=fopen("D:even.txt","w");
fptr_o=fopen("D:/odd.txt","w");
if(fptr==NULL){
printf("Error!");
exit(1);
}
printf("How many record that you want
to store : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&n1);
fprintf(fptr,"%d\n",n1);
if(n1%2!=0)
{
fprintf(fptr_o,"%d\n",n1);
}
else
{
fprintf(fptr_e,"%d\n",n1);
}
}
printf("The data are written to the respective
file\n\n");
fclose(fptr);
fclose(fptr_e);
fclose(fptr_o);
printf("The Even Number are: \t");
fptr_e=fopen("D:/even.txt","r");
while((fscanf(fptr_e,"%d",&n1))!=EOF)
{
printf("%d\t",n1);
}
printf("The Odd Number are: \t");
fptr_o=fopen("D:/odd.txt","r");
while((fscanf(fptr_o,"%d",&n1))!=EOF)
{
printf("%d\t",n1);
}

fclose(fptr);
fclose(fptr_e);
fclose(fptr_o);

}
END

You might also like