C++ program to learn the basics of using a file

C++ program to learn the basics of using a file
This is a program that would help you to learn about the basics of a file through c++. This would teach ue that how can we acess a file, of how can we open it, how can we cloase it, how can we acess the documents present in a file. Its easy to use just install a c++ compiler copy this program to a new notepad and save it as a .cpp file and copy it in the bin of c++ compiler. Its all done now just open compiler search for the file open it and run it.

#include<stdlib.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
void main()
{   clrscr();
    fstream f1("mydata.dat",ios::in);
    char ch;
    int cnt=0;
    char c;
    if(!f1)
    {  cout<<"\n cannot open";
       getch();
       exit(-1);
    }
    cout<<"\n the text document is :";
    while (!f1.eof())
    {  f1.get(c);
       cout<<c;
    }
    f1.close();
    f1.open("mydata.dat",ios::in);
    while (!f1.eof())
    {  f1.get(ch);
       if(isalpha(ch))
       cnt++;
    }
    cout<<"\n number of alphabets are:"<<cnt;
    f1.close();
    getch();
}

C++ program to copy selective text from one file to another

C++ program to copy selective text from one file to another
This is a c++ program with a complete different aspect, it doesn't belong to the common topics. This program can create a file, simultaneously it will clear our concept of opening and closing the same file. Also will help us to know to how to open a file in input mode with the command 'ios::out'. The second switch option is provided to us to just to see what does the file contains in it. This option can help us to learn a different aspect of how o open a file in output mode that is with 'ios::in'. this can also clarify our concept of how to open and read a file simultaneously. The third switch option is to create a complete new file which will contain the vowels of file1. The file1 is ran in a loop if the pointer encounters a vowel then the vowel will be copied to the file2. At the end of the loop both the files have to be closed. As a result the third one will clarify that how can we close two files simultaneously. Basically, the third option teaches us how to deal with two files all together. This was a short discussion on the program.So, without wasting much time lets start with the program. The program is as follows:
#include
#include
#include
#include
#include
void main()
{
clrscr();
int n,j;
fstream ofile,afile;
char str[100];
char ch,ch1;
do
{
cout<<"\n\t1.Create Text\n\t2.Read from File\n\t3.create another file";
cout << "\n 4.Exit  ";
cin>>ch;
switch(ch)
{
    case '1' :
ofile.open("smp.txt",ios::out);
cout<<"\n Enter The Text ";
gets(str);
ofile<<str;
ofile.close();
break;
    case '2' :
char tmp1;
afile.open("smp.txt",ios::in);
while(!afile.eof())
{
afile.get(tmp1);
if(isalpha(tmp1))
{
  if (islower(tmp1))
  {
  if (tmp1=='a'||tmp1=='e'||tmp1=='i'||tmp1=='o'||tmp1=='u')
  cout << "\n Lower case vowel "<<tmp1;
  else
  cout<<"\n Lower case consonants "<<tmp1;
  }
  if (isupper(tmp1))
  {
  if (tmp1=='A'||tmp1=='E'||tmp1=='I'||tmp1=='O'||tmp1=='U')
  cout << "\n Upper case vowel "<<tmp1;
  else
  cout<<"\n Lower case consonants "<<tmp1;
  }
}

}
afile.close();
break;
    case '3' : ofile.open("smp.txt",ios::in);
afile.open("smp1.txt",ios::out);
char c;
while(ofile)
{
ofile.get(c);
c = tolower(c);
if (c=='a'||c=='i'||c=='e'||c=='o'||c=='u')
afile.put(c);
}
ofile.close();
afile.close();

    case '4' : exit(0);
}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
  }while(ch1=='Y'||ch1=='y');
getch();
}

C++ program that would create display and count words,vowel and digits present in a file

C++ program that would create display and count words,vowel and digits present in a file
So are u searching for a c++ based program that would work on the properties of files,you have visited the right place. This is a program that works on the switch case property which handles a set of data in the file simultaneously. Through this program we shall learn how to use the different functions of a file like opening closing or updating it. A very helpfull program to learn about a file in dept. This is a c++ based program that would help in creating a file in the first switch option means it will give you an option of writing text documents in the file. The second switch option is to count the number of vowels or words or digits present in the text presently written in a pre created file. The vowels, words or digits are encountered with the help of "if" case once they are encoutered these are stored in a seperate variable and displayed seperately. The third switch option is to display contents present in the file. This was all about the program so without wasting much time lets start with the program. The disscused program is as follows:
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
void main()
{
clrscr();
int n,j;
fstream ofile,afile;
char str[100];
char ch,ch1;

do
{
cout<<"\n\t1.Create Text\n\t2.Count vowels/words/digits\n\t3.Show Text\n\t4.Exit";
cin>>ch;
switch(ch)
{
    case '1' :
ofile.open("smp.txt",ios::out);
cout<<"\n Enter The Text ";
gets(str);
ofile<<str;
ofile.close();
break;
    case '2' :
char tmp1;
int v=0,d=0,w=0;
afile.open("smp.txt",ios::in);
while(!afile.eof())
{
afile.get(tmp1);

if(tmp1=='a'||tmp1=='e'||tmp1=='i'||tmp1=='o'||tmp1=='u')
v++;
if(isdigit(tmp1))
d++;
if(tmp1==' '||tmp1=='.')
w++;

}
afile.close();
cout<<"\n No of Vowels: "<<v;
cout<<"\n No of digits: "<<d+1;
cout<<"\n No of words: "<<w;
break;
    case '3' :
char tmp2;
afile.open("smp.txt",ios::in);
ofile.open("spl.txt",ios::out);
while(!afile.eof())
{
afile.get(tmp2);
if(tmp2==' ')
{
ofile<<'#';
}
else
{
ofile<<tmp2;
}
}
afile.close();
ofile.close();

cout<<"\nFormatted text:\t";
afile.open("spl.txt",ios::in);
while(afile)
{
afile.get(ch);
cout<<ch;
}
afile.close();
break;
    case '4' : exit(0);
}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
  }while(ch1=='Y'||ch1=='y');
getch();
}

A program to give input and output in a file using switch cases

A program to give input and output in a file using switch cases
This is a c++ program created through switch case in which the user gets a switching option of creating a file and displaying the elements of the file. This program uses the funtions to open and cloase a file. Through this program a student can clarify his\her difficulties in defining a file. In this program new header files too are used such as process.h, fstream.h and few more. File is actually a very essential part of c++ so this can be clarified by this simple and basic program. A student can enhance his\her knowledge in file concept. So friends without wasting much of our time lets start with the program the program is as follows:

#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int n,j;
fstream ofile,afile;
char str[100];
char ch,ch1;

do
{
cout<<"\n\t1.Enter Text\n\t2.Show Text\n\t3.Exit";
cin>>ch;
switch(ch)
{
    case '1' :
ofile.open("smp.txt",ios::out);
cout<<"\nEnter The Text ";
gets(str);
ofile<<str;
ofile.close();
char tmp[20];
afile.open("smp.txt",ios::in);
ofile.open("vwl.txt",ios::out);
while(!afile.eof())
{
afile.getline(tmp,20,' ');
if(tmp[0]=='a'||tmp[0]=='e'||tmp[0]=='i'||tmp[0]=='o'||tmp[0]=='u')
{
ofile<<tmp;
ofile<<' ';
}
}
afile.close();
ofile.close();

break;
    case '2' : cout<<"\nFormatted text:\t";
afile.open("vwl.txt",ios::in);
while(afile)
{
afile.get(ch);
cout<<ch;
}
afile.close();
break;
    case '3' : exit(0);
}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
  }while(ch1=='Y'||ch1=='y');
getch();
}

Program to generate report card of a student using functions like setw and gotoxy

Program to generate report card of a student using functions like setw and gotoxy
This is a c++ program that will take input of employees datails such as name, number and salary. Using switch case this will generate a report card of the student in tabular form. In this program we will learn about a new function "setw()" due to this a new header file is also introduced that is "process.h". this could teach one a good concept of how to create a report card of a student with proper arrangment. Without disscusing much lets work on the program, the program comes as follows:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<iomanip.h>
class employee
{
int eno;
char name[30];
float salary;
public :
void input()
{
cout << "Enter Employee Number ";
cin >>eno;
cout << "Enter name ";
gets(name);
cout << "Enter salary ";
cin >>salary;
}
void show()
{
cout << eno << setw(20)<<name<<setw(20)<<salary<<endl;
}
float rt_sal()
{
return salary;
}
}emp[10];
main()
{
int n,ch,i,j;
char choice;
do
{
clrscr();
cout << "1. For enter "<<endl;
cout << "2. For tabular report"<<endl;
cout << "3. For exit";
cin >> ch;
switch(ch)
{
case 1: cout << "Enter how many employees ";
cin >>n;
for(i=0;i<n;i++)
{
emp[i].input();
}

break;
case 2:
employee temp;
for (i=0;i<n;i++)
{
for(j=i;j<n-1;j++)
{
if (emp[j].rt_sal()>emp[j+1].rt_sal())
{
temp = emp[j];
emp[j]=emp[j+1];
emp[j+1]=temp;
}
}
}
gotoxy(6,6);
cout <<"Employee Number ";
gotoxy(26,6);
cout <<"Name";
gotoxy(46,6);
cout <<"Salary"<<endl;
int r = 8;
for(i=0;i<n;i++)
{
emp[i].show();
r++;
}

break;
case 3: exit(0);

}
cout << "\n Do U want to continue";
cin>>choice;
}while(choice == 'Y' ||choice =='y');

}

A program to clarify the use of files how to open how to close

A program to clarify the use of files how to open how to close
This is a c++ program made using files to generate a report of enggineering student. In this program it will take input of students full info like name, roll and marks.this program includes the concept of opening and cloasing of file. This program also includes a few extra header files and also the concept of switch case too. The program is as follows:
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<ctype.h>
class student
{
char name[30];
int rollno;
int marks;
public:
void input()
{
cout<<"\nEnter Name: ";
gets(name);
cout<<"Enter Rollno.: ";
cin>>rollno;
cout<<"enter marks";
cin>>marks;
}
void display()
{
cout<<"\n"<<name<<"\t"<<rollno<<"\t"<<marks<<"\t";
if(marks>=96)
cout<<"computer sc.";
else if(marks>=91&&marks<=95)
cout<<"Electronics";
else if(marks>=86&&marks<=90)
cout<<"Mechanical";
else if(marks>=81&&marks<=85)
cout<<"Electrical";
else if(marks>=76&&marks<=80)
cout<<"Chemical";
else if(marks>=71&&marks<=75)
cout<<"Civil";
else
cout<<"none";

}

};
void main()
{
clrscr();
student s;
int n,i,j;
fstream ofile,afile;

char ch,ch1;

do
{
cout<<"\n\t1.Add records\n\t2.Show Records\n\t3.Exit\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
    case '1' :
ofile.open("st.dat",ios::app|ios::binary);
cout<<"\nEnter no. of records to be Entered: ";
cin>>n;
for(i=0;i<n;i++)
{
s.input();
ofile.write((char*)&s,sizeof(student));
}
ofile.close();
break;
    case '2' : cout<<"\nName\tRollno\tMarks\tStream";
afile.open("st.dat",ios::in);
while(afile)
{
afile.read((char *)&s,sizeof(student));
if (!afile)
break;
s.display();
}
afile.close();
break;
    case '3' : exit(0);
}
cout<<"\n\t DO U want to continue <Y/N>: ";
cin>>ch1;
  }while(tolower(ch1)!='n');
getch();

}

C++ program to generate an array and to work on it

C++ program to generate an array and to work on it
This is a c++ program which will teach us how to generate an 2D array. This program uses switch case with options such as enter, sort, search , display. This is a simple concept based program just to explain how to work with an array. The header files used are also quite basic. Program is as follows:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],n,i,j,temp;
char ch;

do
{
cout<<"\n\t1.Enter Array\n\t2.Sort Array\n\t3.Search\n\t4.Display\n\t5.Exit";
cin>>ch;
switch(ch)
{
case   '1':
cout<<"\nEnter no. of Elements (<=20): ";
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
break;
case   '2':
cout<<"\nThe Array is Now Sorted";
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
break;

case   '3':
cout<<"\nThe element to be searched";
int el;
cin>>el;
int first=0,last=n-1,mid=0,flag=0;
while(first<=last&&flag==0)
{
mid=(first+last)/2;
if(a[mid]==el)
{
flag=mid;
}
else if(a[mid]<el)
{
first=mid+1;
}
else
{
last=mid-1;
}

}
if(flag>0)
cout<<"\nThe Element is Found at: "<<++flag<<" in the sorted array";
else
cout<<"\n No such Element";
break;
case '4':
cout<<"\n";
for(i=0;i<n;i++)
cout<<a[i]<<ends;



}

}while(ch!='5');
}

Programme to generate a new array using multiple arrays

Programme to generate a new array using multiple arrays
This is a C++ program which will take input of multiple arrays and will get sum up to generate a new array. We have also under taken few basic commands such as "gotoxoy" to arrange the output. The header files used in this are "iostream.h","conio.h","stdio.h","process.h". These are few basic commands whose perfect use can be learnt by a C++ student while viewing this post. This program can enhance ones knowledge related to process.h header file as it is not used more frequently. The proram is as follows:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct student
{
char name[30];
int roll;
int mark1;
int mark2;
int mark3;
int total;
}st[100];
main()
{
int n,ch,i,j;
char choice;
do
{
clrscr();
cout << "1. For enter "<<endl;
cout << "2. For tabular report"<<endl;
cout << "3. For Report card"<<endl;
cout << "4. For exit";
cin >> ch;
switch(ch)
{
case 1: cout << "Enter how many students ";
cin >>n;
for(i=0;i<n;i++)
{
cout << "Enter name ";
gets(st[i].name);
cout << "Enter Roll Number ";
cin >>st[i].roll;
cout << "Enter Mark1 ";
cin >>st[i].mark1;
cout << "Enter Mark2 ";
cin >> st[i].mark2;
cout << "Enter Mark3 ";
cin >> st[i].mark3;
st[i].total = st[i].mark1+st[i].mark2+st[i].mark3;
}

break;
case 2:
student temp;
for (i=0;i<n;i++)
{
for(j=i;j<n-1;j++)
{
if (st[j].total>st[j+1].total)
{
temp = st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}
gotoxy(6,6);
cout <<"Name ";
gotoxy(16,6);
cout <<"Roll";
gotoxy(26,6);
cout <<"Mark1";
gotoxy(36,6);
cout <<"Mark2";
gotoxy(46,6);
cout <<"Mark3";
gotoxy(56,6);
cout <<"Total";
int r = 8;
for(i=0;i<n;i++)
{
gotoxy(6,r);
cout <<st[i].name;
gotoxy(16,r);
cout <<st[i].roll;
gotoxy(26,r);
cout <<st[i].mark1;
gotoxy(36,r);
cout <<st[i].mark2;
gotoxy(46,r);
cout <<st[i].mark3;
gotoxy(56,r);
cout <<st[i].total;
r++;
}

break;
case 3: int troll;
cout << "\nEnter the roll number to be searched ";
cin >> troll;
for(i=0;i<n;i++)
{
if (st[i].roll == troll)
{
cout << " \n Name "<<st[i].name;
cout << "\n Roll "<< st[i].roll;
cout << "\n Mark1 "<<st[i].mark1;
cout << "\n Mark2 "<<st[i].mark2;
cout << "\n Mark3 "<<st[i].mark3;
cout << "\n total "<<st[i].total;
}
}
break;
case 4: exit(0);

}
cout << "\n Do U want to continue";
cin>>choice;
}while(choice == 'Y' ||choice =='y');
}

C++ Program to generate report card of a student as per entry of marks

C++ Program to generate report card of a student as per entry of marks
This C++ based program basically will take enrty of a students name, roll number and his/her marks in atleast 3 subjects and generate a report card of that students performance. This program has been desinged using arrays. This program undertakes multiple cases so switch cases has been used to simplify it. This program has also undertaken the concept of strings with the header file <string.h> this will help in enhancing a students mind that how well can three arrays be used to generate a new array. The program is as follows:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main()
{
clrscr();
char *name[50],*nm;
int rno[50],m1[50],m2[50],m3[50],tot[50],i,n,trn;
cout<<"Enter no. of records: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Name of Student "<<i<<": ";
gets(name[i]);
cout<<"Enter Roll No. of Student "<<i<<": ";
cin>>rno[i];
cout<<"Enter Mark1 of Student "<<i<<": ";
cin>>(m1[i]);
cout<<"Enter Mark2 of Student "<<i<<": ";
cin>>(m2[i]);
cout<<"Enter Mark3 of Student "<<i<<": ";
cin>>(m3[i]);
tot[i]=m1[i]+m2[i]+m3[i];
}
char ch,ch1;
int flag=0;
cout<<"\n\t1.Report card for particular student"
"\n\t2.List all records\n\t3.Exit";
cin>>ch;
do
{
switch(ch)
{
case '1':
cout<<"\nEnter the Roll no.";
cin>>trn;
flag=0;
for(i=0;i<n;i++)
if(trn==rno[i])
{
cout<<"\n Name: "<<name[i]<<"\t";
cout<<"Roll No.: "<<rno[i];
cout<<"\nMark1: "<<m1[i];
cout<<"\nMark2: "<<m2[i];
cout<<"\nMark3: "<<m3[i];
cout<<"\n----------";
cout<<"\nTotal: "<<tot[i];
flag=1;

}
if(flag==0)
cout<<"Record not found";
break;
case '2':
cout<<"\nRollno\tName     \tMark1\tMark2\tMark3";
cout<<"\tTotal";
cout<<"\n------\t----------\t----\t----\t----";
cout<<"\t-----";
for(i=0;i<n;i++)
{
cout<<"\n"<<rno[i]<<"\t"<<name[i]<<"\t"
<<m1[i]<<"\t"<<m2[i]<<"\t"<<m3[i]<<"\t"
<<tot[i];
}
break;
}
cout<<"\n\t1.Report card for particular student"
"\n\t2.List all records\n\t3.Exit";
cin>>ch;


}while(ch!='3');
getch();
}

C++ program to create a 2D array

C++ program to create a 2D array
This Program will take entry of array row wise and column wise and will generate a two dimensional array like for example you have been asked to create a C++ program that could take in names of 10 cities each of at least 50 letters so who will you do at this time itself you have to go for a 2-D array. The program for 2-D array creation is as follows:

#include<iostream.h>
#include<conio.h>
void twodigit(int str[10][10],int a,int b)
{
int i,j,flag=0;
cout<<"The Two digit Numbers are: ";
for(i=0;i<a;i++)
for(j=0;j<b;j++)
{
if(str[i][j]>=10&&str[i][j]<=99)
{
cout<<str[i][j]<<ends;
flag=1;
}

}
if(flag==0)
cout<<"None";
}
void main()
{
clrscr();
int str[10][10],i,j,a,b;
cout<<"\n EnterNo. of rows: ";
cin>>a;
cout<<"\nEnter the no. of columns: ";
cin>>b;
cout<<"\nEnter the Elements of the array: ";
for(i=0;i<a;i++)
for(j=0;j<b;j++)
cin>>str[i][j];
twodigit(a,a,b);
getch();
}