Sunday 28 April 2013

c++ program save the string in file and read the string from the file


  1. main()//programmingisbest.blogspot.com
  2. {
  1. char name[15];//programmingisbest.blogspot.com//programmingisbest.blogspot.com
  2. char telephn[10];//programmingisbest.blogspot.com//programmingisbest.blogspot.com
  3. char city[15],opt;
  4. ofstream xyz("nnn.dat");//programmingisbest.blogspot.com
  5. int rec =1;//programmingisbest.blogspot.com
  6. clrscr();
  7. if(!xyz)//programmingisbest.blogspot.com
  8. {
  9.  cout<<"file is not open";
  10.  exit(1);
  11. }
  12. opt='y';
  13. while(opt=='y' || opt=='Y')
  14. {
  15. cout<<"\n Enter the record no # : "<<rec;
  16. cout<<"\n Enter the name of the customer: ";
  17. cin>>name;
  18. cout<<"\n Enter the telephone no of the customer : ";
  19. cin>>telephn;
  20. cout<<"\n enter the city of the customer : ";
  21. cin>>city;
  22. xyz<<name<<"\t"<<telephn<<"\t"<<city<<endl;
  23. rec ++;
  24. cout<<"\n enter more record press (Y/n)";
  25. cin>>opt;
  26. }
  27. }














  1. #include<iostream.h>
  2. #include<fstream.h>
    1. #include<conio.h>
    2. #include<stdlib.h>
    3. main()
    4. {
    5. char name[15];//programmingisbest.blogspot.com
    6. char telephn[10];
    7. char city[15];//programmingisbest.blogspot.com
    8. int rec =1;
    9. ifstream xyz("nnn.dat");
    10. clrscr();//programmingisbest.blogspot.com
    11. if(!xyz)
    12. {
    13.  cout<<"file is not open";
    14.  exit(1);
    15. }
    16. while(xyz)
    17. {
    18. xyz>>name>>telephn>>city;//programmingisbest.blogspot.com
    19. cout<<"\nrecod no is # : "<<rec;//programmingisbest.blogspot.com
    20. cout<<"\nName is : "<<name;//programmingisbest.blogspot.com
    21. cout<<"\ntelephone no is : "<<telephn;
    22. cout<<"nCity is : "<<city;//programmingisbest.blogspot.com
    23. rec++;//programmingisbest.blogspot.com
    24. }
    25. }



Character Write adn Read Program in which character by character Data is saved to the file and then Read from the file in turboo 's bin folder

//program stores characters until escape key is not pressed

#include<iostream.h>
#include<fstream.h>
#include<conio.h>

void main()
{
char ch;
ofstream as("test.txt");
clrscr();
while(ch!=27)
{
ch = getche();
//obj.put(character);
as.put(ch);
if(ch=='\r')
cout<<endl;
}
}

//read character by character data from the file :D
//umarjaved01@gmail.com

#include<iostream.h>
#include<fstream.h>
#include<conio.h>

void main()
{
char ch;
ifstream as("test.txt");
clrscr();
while(as)
{
as.get(ch);
cout<<ch;
}
}