Category Archives: C Programming

File I/O in C – Part 3

1. Explain File I/O.

2. Random Access File Method.

a. int fseek(FILE *FP, long offset, int orgin);

b. void rewind(fp);

c. long ftell(fp);

3. Example showing the use of Random Access File Method in C.


/*
Developed By:Prof. Vinod Pillai
vinodthebest@gmail.com
File in C - Part 3.
*/

#include
#include
#include

struct emp
{
int id;
char name[20];
int salary;
};

void append();
void display();
void displayAll();
void rname();
void rremove();

void getFirstRecord();
void getLastRecord();
void getRandomRecord();

char fname[]={"one.dat"};

int main()
{
int ch;

while(1)
{
printf("========================================================\n\n");

printf("1. Append\n\n");

printf("2. Get First Record\n\n");
printf("3. Get Last Record\n\n");
printf("4. Get particular position value\n\n");

printf("5. Display\n\n");
printf("6. Display All\n\n");
printf("7. Rename\n\n");
printf("8. Delete File\n\n");
printf("0. Exit\n\n");

printf("========================================================\n\n");

printf("\nPlease enter your Choice:");
scanf("%d",&ch);

switch(ch)
{
case 1: append();
break;

case 2: getFirstRecord();
break;

case 3: getLastRecord();
break;

case 4: getRandomRecord();
break;

case 5: display();
break;

case 6: displayAll();
break;

case 7: rname();
break;

case 8: rremove();
break;

case 0: exit(0);
}
}

return 0;
}

//Add New Record to File
void append()
{
FILE *fp;
struct emp t1;

fp=fopen(fname,"ab");

printf("\nEnter ID:");
scanf("%d",&t1.id);
printf("\nEnter name:");
scanf("%s",t1.name);
printf("\nEnter salary:");
scanf("%d",&t1.salary);
fwrite(&t1,sizeof(t1),1,fp);

fclose(fp);
}

//Rename File
void rname()
{
char name[20];

printf("\nEnter the New File Name:");
fflush(stdin);
scanf("%[^\n]",name);

rename(fname,name);

strcpy(fname,name);
}

//Remove Database File
void rremove()
{
FILE *fp,*fp1;
struct emp t;

char name[20];
char val[20];

printf("\nDo you want to make copy of it (Y/N):");
scanf("%s",val);

if(strcmp(val,"Y")==0)
{

printf("\nEnter the New File Name:");
fflush(stdin);
scanf("%[^\n]",name);

fp=fopen(name,"wb");
fp1=fopen(fname,"rb");

while(1)
{
fread(&t,sizeof(t),1,fp1);

if(feof(fp1))
{
break;
}
fwrite(&t,sizeof(t),1,fp);
}

fclose(fp);
fclose(fp1);

remove(fname);

strcpy(fname,name);
}
else
{
remove(fname);
}
}

//Random Access File
void getFirstRecord()
{
FILE *fp;
struct emp t;

fp=fopen(fname,"rb");

rewind(fp);

fread(&t,sizeof(t),1,fp);

printf("\n========================================================\n\n");
printf("\t\t All Employee Details\n\n");
printf("========================================================\n\n");

printf("ID\tName\tSalary\n\n");

printf("%d\t",t.id);
printf("%s\t",t.name);
printf("%d\t\n\n",t.salary);

fclose(fp);
}

void getLastRecord()
{
FILE *fp;
struct emp t;

fp=fopen(fname,"rb");

fseek(fp,-sizeof(t),2);

fread(&t,sizeof(t),1,fp);

printf("\n========================================================\n\n");
printf("\t\t All Employee Details\n\n");
printf("========================================================\n\n");

printf("ID\tName\tSalary\n\n");

printf("%d\t",t.id);
printf("%s\t",t.name);
printf("%d\t\n\n",t.salary);

fclose(fp);
}

void getRandomRecord()
{
FILE *fp;
struct emp t;
int position,size,count,i;

fp=fopen(fname,"rb");

printf("Enter the position you want to display:");
scanf("%d",&position);

fseek(fp,0,2);
size=ftell(fp);
count=size/sizeof(t);

fseek(fp,0,0);
printf("\n Total object is:%d",count);

for(i=1;i<=count;i++)
{
fread(&t,sizeof(t),1,fp);

if(i==position)
{
printf("\n Current object is:%d",i);
break;
}
}
fclose(fp);

printf("\n========================================================\n\n");
printf("\t\t All Employee Details\n\n");
printf("========================================================\n\n");

printf("ID\tName\tSalary\n\n");

printf("%d\t",t.id);
printf("%s\t",t.name);
printf("%d\t\n\n",t.salary);

}

//Display & Display All Details
void display()
{
FILE *fp;
struct emp t;
int id,found=0;

fp=fopen(fname,"rb");

printf("\nEnter the Emp ID:");
scanf("%d",&id);

while(1)
{
fread(&t,sizeof(t),1,fp);

if(feof(fp))
{
break;
}
if(t.id==id)
{
found=1;
printf("\n========================================================\n\n");
printf("\t\t Employee Details of %d\n\n",t.id);
printf("========================================================\n\n");

printf("Name\tSalary\n\n");

printf("%s\t",t.name);
printf("%d\t\n\n",t.salary);

printf("========================================================\n\n");
}
}
if(found==0)
{
printf("\nSorry No Record Found");
}
fclose(fp);
}

void displayAll()
{
FILE *fp;
struct emp t;

fp=fopen(fname,"rb");

printf("\n========================================================\n\n");
printf("\t\t All Employee Details\n\n");
printf("========================================================\n\n");

printf("ID\tName\tSalary\n\n");

while(1)
{
fread(&t,sizeof(t),1,fp);

if(feof(fp))
{
break;
}
printf("%d\t",t.id);
printf("%s\t",t.name);
printf("%d\t\n\n",t.salary);

}
printf("========================================================\n\n");

fclose(fp);
}

Advertisements

1 Comment

Filed under C Programming, File I/O

Array in C

Array: An array is a collective name given to a group of similar quantities. If we would like to store 100 employee salary then we will should create an array of size 100 (int salary[100]) These similar elements could be all integers or all floats or all characters etc.

In this video I have explained about Array in C:

1. Explain Array & its need.

2. Code for entering & gettting value from Array in C.

3. Type of Array.

a. Single Dimenstion Array.

b. Multi Dimenstion Array.

Secondly I have shown code explaining how you can entering & gettting value from Array in C.


/*
Developed By:Prof. Vinod Pillai
vinodthebest@gmail.com
Array in C.
*/

#include

int main()
{
//Single Dimenstion Array
int array[5];
int i,j,rel=0;
//Multi Dimenstion Array 3 Rows & 5 Coloums each
int arr[3][5];

printf("\n Enter the Value of Array:");
scanf("%d",&array[0]);

printf("\n The result is :%d",array[0]);

//Getting the Value
for(i=0;i<5;i++)
{
printf("\n Enter the Value of Array:");
scanf("%d",&array[i]);
}

//Printing the Value
for(i=0;i<5;i++)
{
printf("\n The result is :%d",array[i]);
rel=rel+array[i];
}

printf("\nThe sum of Array values is:%d",rel);

//***************************************************************
rel=0;
//Two Dimenstion Array Getting the Value
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("\n Enter the Value of Array:");
scanf("%d",&arr[i][j]);
}
}

//Printing the Value
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("\n The result is :%d",arr[i][j]);
rel=rel+arr[i][j];
}
}

printf("\n\nThe sum of Array values is:%d",rel);
return 0;
}

Leave a comment

Filed under C Programming, General

File I/O in C – Part 2

Part 2:

1. Explain File I/O.

2. Code for writing and reading values:

a. Character values. [fgetc & fputc]

b. String values. [fprintf & fscanf]

c. Integer value. [putw & getw]

d. Structure value. [fread & fwrite]


/*
Developed By:Prof. Vinod Pillai
vinodthebest@gmail.com
File in C - Part 2.
*/

#include

struct Student
{
int id;
int marks;
};

int main()
{
FILE *fp;

int ivalue,irel;
float fvalue,frel;
char cvalue,crel;
char strvalue[20],strrel[20];
struct Student svalue,srel;

//-------------------------------------------------------------------
fp=fopen("one.txt","w");

//Character Value Read & Write [fputc & fgetc]
//Write:
printf("\n Enter the Character Value:");
scanf("%c",&cvalue);

fputc(cvalue,fp);

fclose(fp);

//Read:
fp=fopen("one.txt","r");

crel=fgetc(fp);

printf("\n The Character value is :%c\n",crel);

fclose(fp);
//-------------------------------------------------------------------
//String Value Read & Write [fprintf & fscanf]
//Write:
fp=fopen("one.txt","w");

printf("\n Enter the String Value:");
scanf("%s",strvalue);

fprintf(fp,"%s",strvalue);

fclose(fp);

//Read:
fp=fopen("one.txt","r");

fscanf(fp,"%s",strrel);

printf("\n The String value is :%s\n",strrel);

fclose(fp);

//-------------------------------------------------------------------
//Structure Value Read & Write [fwrite & fread]
//Write:
fp=fopen("one.dat","wb");

printf("\nEnter the ID:");
scanf("%d",&svalue.id);

printf("\nEnter the Marks:");
scanf("%d",&svalue.marks);

fwrite(&svalue,sizeof(svalue),1,fp);

fclose(fp);

//Read:
fp=fopen("one.dat","rb");

fread(&srel,sizeof(srel),1,fp);

printf("\nThe ID is:%d\n",srel.id);
printf("\nThe Marks is:%d\n",srel.marks);

fclose(fp);
return 0;
}

Leave a comment

Filed under C Programming, File I/O

File I/O in C – Part 1

File I/O: We have been using the functions such as printf and scanf to read and write data. This works fine as long as data is small. The entire data is lost when either the program is terminated or the computer is turned off. So it is necessary to have more flexible approach where data can be stored on the disks and read whenever necessary, without destroying data. This concept is called files.

FILE is a structure declared in stdio.h . We have to use file pointer, a pointer variable that points to a structure FILE.

Part 1:

1. Explain File I/O & its need.

2. Major operations with Files in C.

a. Creating a File.

b. Opening a File.

c. Closing a File.

d. Reading & Writing data to and from a File.

3. Modes for File.

4. Code for writing and reading integer values from Files in C.


/*
Developed By:Prof. Vinod Pillai
vinodthebest@gmail.com
File in C - Part 1.
*/

#include

int main()
{
FILE *fp;
int value,rel;

//Creating new file with Write Mode
fp=fopen("one.txt","w");

printf("\n Enter the Value to write:");
scanf("%d",&value);

//Writing value to File.
putw(value,fp);

//Closing the File from Write Mode.
fclose(fp);

//Opening file with Read Mode
fp=fopen("one.txt","r");

//Reading value from File.
rel=getw(fp);

printf("\n The result is :%d",rel);

//Closing the File from Write Mode.
fclose(fp);

return 0;
}

3 Comments

Filed under C Programming, File I/O

Structure in C

Structure: Structure is user defined data type which is used to store heterogeneous data under unique name. Keyword ‘struct’ is used to declare structure. The variables which are declared inside the structure are called as ‘members of structure’.

I have explained about Structure in C:

1. Explain Structure & its need.

2. Code for creating simple Structure in C.

3. Code for creating variables of structure.

4. Nested structure.


/*
Developed By:Prof. Vinod Pillai
vinodthebest@gmail.com
Structure in C
*/

#include

//--------Structure Declaration---------
struct Student
{
int id;
int marks;
};

int main()
{
//Structure variables.
struct Student s1,s2;

//First Student Data
printf("\nEnter the ID:");
scanf("%d",&s1.id);

printf("\nEnter the Marks:");
scanf("%d",&s1.marks);

//Display Student Data
printf("\nThe ID is:%d",s1.id);
printf("\nThe Marks is:%d",s1.marks);

//--------Similarly Second Student Data---------

printf("\nEnter the ID:");
scanf("%d",&s2.id);

printf("\nEnter the Marks:");
scanf("%d",&s2.marks);

printf("\nThe ID is:%d",s2.id);
printf("\nThe Marks is:%d",s2.marks);

return 0;
}

Leave a comment

Filed under C Programming

How to Store Structures Data in C

In this video I have shown how you can mange User defined data type in C using the concept of Structure and File I/O. For data storage I have used File I/O and internally used the concept of Structure.

  • How to create Structure.
  • How to Store Structure in Files.
  • How to Update Structure values stored in Files.
  • How to Delete Structure values stored in Files.
  • How to Search particular record in Files.
  • How to rename & delete files in C.

Complete Code:

#include
#include
#include

struct emp
{
int id;
char name[20];
int salary;
};

void append();
void display();
void displayAll();
void modify();
void del();
void search();

void rname();
void rremove();

char mygetch();

char fname[]={"mydb.dat"};

int main()
{
int ch;

while(1)
{
system("clear"); //clrscr();

printf("==================Employee Management System=============\n\n");

printf("1. Append\n\n");
printf("2. Modify\n\n");
printf("3. Delete\n\n");
printf("4. Search\n\n");
printf("5. Display\n\n");
printf("6. Display All\n\n");
printf("7. Rename\n\n");
printf("8. Delete File\n\n");
printf("0. Exit\n\n");

printf("========================================================\n\n");

printf("\nPlease enter your Choice:");
scanf("%d",&ch);

switch(ch)
{
case 1: append();
break;

case 2: modify();
break;

case 3: del();
break;

case 4: search();
break;

case 5: display();
break;

case 6: displayAll();
break;

case 7: rname();
break;

case 8: rremove();
break;

case 0: exit(0);
}

mygetch();
}

return 0;
}
void append()
{
FILE *fp;
struct emp t1;

fp=fopen(fname,"ab");

printf("\nEnter ID:");
scanf("%d",&t1.id);
printf("\nEnter name:");
scanf("%s",t1.name);
printf("\nEnter salary:");
scanf("%d",&t1.salary);
fwrite(&t1,sizeof(t1),1,fp);

fclose(fp);
}

void rname()
{
char name[20];

printf("\nEnter the New File Name:");
fflush(stdin);
scanf("%[^\n]",name);

rename(fname,name);

strcpy(fname,name);
}

void rremove()
{
FILE *fp,*fp1;
struct emp t;

char name[20];
char val[20];

printf("\nDo you want to make copy of it (Y/N):");
scanf("%s",val);

if(strcmp(val,"Y")==0)
{

printf("\nEnter the New File Name:");
fflush(stdin);
scanf("%[^\n]",name);

fp=fopen(name,"wb");
fp1=fopen(fname,"rb");

while(1)
{
fread(&t,sizeof(t),1,fp1);

if(feof(fp1))
{
break;
}
fwrite(&t,sizeof(t),1,fp);
}

fclose(fp);
fclose(fp1);

remove(fname);

strcpy(fname,name);
}
else
{
remove(fname);
}
}

void modify()
{
FILE *fp,*fp1;
struct emp t,t1;
int id,found=0,count=0;

fp=fopen(fname,"rb");
fp1=fopen("temp.dat","wb");

printf("\nEnter the Emp ID you want to Modify:");
scanf("%d",&id);

while(1)
{
fread(&t,sizeof(t),1,fp);

if(feof(fp))
{
break;
}
if(t.id==id)
{
found=1;
printf("\nEnter Employee ID:");
scanf("%d",&t.id);

fflush(stdin);
printf("\nEnter Employee Name:");
scanf("%s",t.name);
printf("\nEnter Employee Salary:");
scanf("%d",&t.salary);
fwrite(&t,sizeof(t),1,fp1);
}
else
{
fwrite(&t,sizeof(t),1,fp1);
}
}
fclose(fp);
fclose(fp1);

if(found==0)
{
printf("Sorry No Record Found\n\n");
}
else
{
fp=fopen(fname,"wb");
fp1=fopen("temp.dat","rb");

while(1)
{
fread(&t,sizeof(t),1,fp1);

if(feof(fp1))
{
break;
}
fwrite(&t,sizeof(t),1,fp);
}

}
fclose(fp);
fclose(fp1);
}

void del()
{
FILE *fp,*fp1;
struct emp t,t1;
int id,found=0,count=0;

fp=fopen(fname,"rb");
fp1=fopen("temp.dat","wb");

printf("\nEnter the Emp ID you want to Delete:");
scanf("%d",&id);

while(1)
{
fread(&t,sizeof(t),1,fp);

if(feof(fp))
{
break;
}
if(t.id==id)
{
found=1;
}
else
{
fwrite(&t,sizeof(t),1,fp1);
}
}
fclose(fp);
fclose(fp1);

if(found==0)
{
printf("Sorry No Record Found\n\n");
}
else
{
fp=fopen(fname,"wb");
fp1=fopen("temp.dat","rb");

while(1)
{
fread(&t,sizeof(t),1,fp1);

if(feof(fp1))
{
break;
}
fwrite(&t,sizeof(t),1,fp);
}
}
fclose(fp);
fclose(fp1);
}

void display()
{
FILE *fp;
struct emp t;
int id,found=0;

fp=fopen(fname,"rb");

printf("\nEnter the Emp ID:");
scanf("%d",&id);

while(1)
{
fread(&t,sizeof(t),1,fp);

if(feof(fp))
{
break;
}
if(t.id==id)
{
found=1;
printf("\n========================================================\n\n");
printf("\t\t Employee Details of %d\n\n",t.id);
printf("========================================================\n\n");

printf("Name\tSalary\n\n");

printf("%s\t",t.name);
printf("%d\t\n\n",t.salary);

printf("========================================================\n\n");
}
}
if(found==0)
{
printf("\nSorry No Record Found");
}
fclose(fp);
}

void search()
{
FILE *fp;
struct emp t;
int found=0;
char name[20];

fp=fopen(fname,"rb");

printf("\nEnter the Employee Name:");
scanf("%s",&name);

while(1)
{
fread(&t,sizeof(t),1,fp);

if(feof(fp))
{
break;
}
if(strcmp(name,t.name)==0)
{
printf("\n========================================================\n\n");
printf("\t\t Employee Details of %d\n\n",t.id);
printf("========================================================\n\n");

printf("Name\tSalary\n\n");

printf("%s\t",t.name);
printf("%d\t\n\n",t.salary);

printf("========================================================\n\n");

}
}
if(found==0)
{
printf("\nSorry No Record Found");
}
fclose(fp);
}
void displayAll()
{
FILE *fp;
struct emp t;

fp=fopen(fname,"rb");

printf("\n========================================================\n\n");
printf("\t\t All Employee Details\n\n");
printf("========================================================\n\n");

printf("ID\tName\tSalary\n\n");

while(1)
{
fread(&t,sizeof(t),1,fp);

if(feof(fp))
{
break;
}
printf("%d\t",t.id);
printf("%s\t",t.name);
printf("%d\t\n\n",t.salary);

}
printf("========================================================\n\n");

fclose(fp);
}

char mygetch()
{
char val;
char rel;

scanf("%c",&val);
scanf("%c",&rel);
return (val);
}

2 Comments

Filed under C Programming

Shopping Cart application in C using Structure & File I/O


In this video I have shown how you can mange purchase product in your shopping cart in C using the concept of Structure & File I/O. For data storage I have used File I/O and internally used the concept of Structure.

  • How to create Structure.
  • How to Store Structure in Files.
  • How to manage Purchase Product List in C.
  • Shopping Card application in C.

Leave a comment

Filed under C Programming