Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space.
#include<iostream> #include<fstream> #include<stdio> #include<string> using namespace std; int main() { ofstream of; // of is a object of ofstream file. of.open("hasib.txt"); // open a text document file named “hasib” to write. char c; ifstream inf("santo.txt"); // open a text document file named “santo” to read. inf.get(c); // retrieve a single character from “santo” file. while(inf) // is character is ‘/0’ ? if not go inside loop { if(c==' ') // is character is a space? If yes execute “if” statement. { while(inf) // is character is ‘/0’ ? if not go inside loop { inf.get(c); // retrieve a single character from “santo” file. if(c!=' ') // is character is not a space? { // If yes go outside the while loop. break; // this is just for skip the space. } } of<<" "; // write single space to “hasib” text document file. of<<c; // write next charcter to “hasib” text document file. } else of<<c; // write single character where no space exits. inf.get(c); // retrieve a single character from “santo” text document again. } return 0; }
Note: Before you run this program you just create a ‘.txt’ document to a particular drive where your compiler was installed.
A file contains a list of telephone numbers in the following form
John 23456
Ahmed 9876
…………… …………
the names contain only one word and the names and telephone numbers are separated by white spaces. Write program to read this file and output the list in two columns. The names should be left justified and the numbers should be right justified.
#include<iostream> #include<fstream> #include<string> #include<iomanip> using namespace std; int main() { char name[100],n[100],number[100]; ifstream santo("robin.txt"); cout<<" Enter your desired name to find mobile number :" ; cin>>n; again: santo>>name; if(!strcmp(name,n)) { santo.getline(number,100); cout<<setw(-20)<<name<<setw(25)<<number<<"\n"; } else { if(santo.eof()!=0) cout<<" Sorry your input name is not found in list \n"; else goto again; } return 0; }
Note: You just create a ‘.txt’ document like as question no 11.1 which contains peoples name and phone number.
OUTPUT
Enter your desired name to find mobile number : john
john 23456
Write a program that will create a data file containing the list of telephone numbers given in exercise in 11.2. Use a class object to store each set of data.
#include<iostream> #include<fstream> #define size 5 using namespace std; class phone { public: void set_data(); }; void phone:: set_data() { ofstream santo("phone.txt"); char *name[size]={"sattar","santo","kamruzzaman","robin","kawser"}; char *number[size] ={"01673050495","01723783117","01818953250","+214324513","+455652132"}; for(int i=0;i<size;i++) { santo.setf(ios::left,ios::adjustfield); santo.width(20); santo<<name[i]; santo.setf(ios::right,ios::adjustfield); santo.width(15); santo<<number[i]<<"\n"; } } int main() { phone book; book.set_data(); return 0; }
Note: When you run this program, It will create a txt document named ‘phone’.
Write an interactive, menu-driven program that will access the file created in exercise 11.3 and implement the tasks:
- (a) Determine the telephone number of the specified person.
- (b) Determine the name if a telephone number is known.
- (c) Update, the telephone number, whenever there is a change.
#include<iostream> #include<fstream> #include<string> #include<iomanip> using namespace std; int main() { char name[100],n[100],m[100],number[100]; ifstream santo("robin.txt"); int test; cout<<" Press 1 to find mobile number of specified person\n " <<" Press 2 to find name of specified number \n" <<" Press 3 to update number \n" <<" What is your option ?: "; cin>>test; if(test==1) { cout<<" Enter the desired name : "; cin>>n; cout<<"\n"; again1: santo>>name; if(!strcmp(name,n)) { santo.getline(number,100); cout<<setw(-20)<<name<<setw(25)<<number<<"\n"; } else { if(santo.eof()!=0) cout<<" Sorry your input name is not found in list \n"; else goto again1; } } else if(test==2) { cout<<" Enter the desired number : "; cin>>n; cout<<"\n"; again2: santo>>name; santo>>number; if(!strcmp(number,n)) { cout<<setw(-20)<<number<<setw(25)<<name<<"\n"; } else { if(santo.eof()!=0) cout<<" Sorry your input number is not found in list \n"; else goto again2; } } else if(test == 3) { ofstream hasib("modified.txt"); cout<<"Enter the name whose number have to change : "; cin>>n; again3: santo>>name>>number; if(!strcmp(n,name)) { cout<<" Enter changed mobile number of"<<name<<": "; cin>>m; hasib.setf(ios::left,ios::adjustfield); hasib.width(20); hasib<<name; hasib.setf(ios::right,ios::adjustfield); hasib.width(15); hasib<<m<<"\n"; while(santo) { santo>>name>>number; hasib.setf(ios::left,ios::adjustfield); hasib.width(20); hasib<<name; hasib.setf(ios::right,ios::adjustfield); hasib.width(15); hasib<<number<<"\n"; } } else { if(santo.eof()!=0) cout<<" Sorry your input name is not available \n"; else { hasib.setf(ios::left,ios::adjustfield); hasib.width(20); hasib<<name; hasib.setf(ios::right,ios::adjustfield); hasib.width(15); hasib<<number<<"\n"; goto again3; } } } return 0; }
During First Run :
OUTPUT
Press 1 to find mobile number of specified person
Press 2 to find name of specified number
Press 3 to update number
What is your option ?: 1
Enter the desired name : john
john 23456
During Second Run :
OUTPUT
Press 1 to find mobile number of specified person
Press 2 to find name of specified number
Press 3 to update number
What is your option ?: 3
Enter the name whose number have to change : Ahmed
Enter changed mobile number of Ahmed : 9876
Next Previous