Posts

Showing posts from November, 2020

3.Modify above program to do following operations. (Write new program) 1) Display current contents of the file. 2) Add one more item at the end of file. 3) Again display contents of appended file. 4) Display total no. of objects in the file. 5) Display total no. of bytes in the file. 6) Take item no. from the user, modify the details of that item. 7) Again display contents of modified file. 8) Use proper error handling functions to prevent errors

 /*J_5_P_3 Modify above program to do following operations. (Write new program) 1) Display current contents of the file. 2) Add one more item at the end of file. 3) Again display contents of appended file. 4) Display total no. of objects in the file. 5) Display total no. of bytes in the file. 6) Take item no. from the user, modify the details of that item. 7) Again display contents of modified file. 8) Use proper error handling functions to prevent errors */ #include <iostream.h> #include <conio.h> #include <fstream.h> #include <string.h> int main() {     clrscr();     char ch;     ifstream in;     in.open("inventory.txt");     while ((ch = in.get()) != EOF)     { cout << ch;     }     streampos begin, end;     in.open("inventory.txt");     begin = in.tellg();     in.seekg(0, ios::end);     end = in.tellg();     in.close();     cout << "size is : " << (end-begin) << " bytes.\n";     getch();     r

2.Write a program of an inventory system. Define one class inventory having data members as 1. int code; 2. char itemname[10]; 3. float cost; and define required member functions. Define main function to read 10 items from the user and write that data in a file. Read that file and display information of all 10 items using proper output formatting.

/* j_5_p_2 Write a program of an inventory system. Define one class inventory having data members as 1. int code; 2. char itemname[10]; 3. float cost; and define required member functions. Define main function to read 10 items from the user and write that data in a file. Read that file and display information of all 10 items using proper output formatting. */ #include <iostream.h> #include <conio.h> #include <fstream.h> #include <string.h> #include <iomanip.h> class inventory { public: int code[10]; char itemname[10][10]; float cost[10]; public: void getdata() { ofstream out("inventory.txt"); out <<"\n-----------------------------------------------\n"; out << "\tcode\titemname\tcost"<< endl; out <<"-----------------------------------------------\n"; cout.precision(2); out.precision(2); for (int i = 0; i < 5; i++) { cout << "Ent

8.Write a c++ program, which accepts two filename from command line appends the content of second file to first file. It should also display the content of the first file after appending.

 /*J_5_P_8 Write a c++ program, which accepts two filename from command line appends the content of second file to first file. It should also display the content of the first file after appending. */ #include <bits/stdc++.h>  using namespace std;     int main()  {      fstream file;          ifstream ifile("file2.txt", ios::in);      ofstream ofile("file.txt", ios::out | ios::app);          if (!ifile.is_open()) {          cout << "file not found";      }      else {          ofile << ifile.rdbuf();      }      string word;         file.open("file2.txt");         while (file >> word) {          cout << word << " ";      }         return 0;  } 

8.Modify the program - 7 to fill the unused spaces with @ signs.

 /*J_4_P_8 Modify the program 7  to fill the unused spaces with @ signs. */ #include <iostream.h> #include <iomanip.h> #include <string.h> #include <conio.h> class item {         char name[40];         int code;         float cost; public:         void get_data(char *n, int c, float co)         {                 strcpy(name, n); code = c; cost = co; } void display(); }; void item::display() { cout.precision(2); cout << setfill('@'); cout.setf(ios::fixed,ios::floatfield); cout.setf(ios::showpoint); cout.setf(ios::left, ios::adjustfield); cout << setw(40) << name << code; cout.setf(ios::right, ios::adjustfield); cout << setw(15) << cost << endl; } void main() { clrscr(); item a[5]; cout << setfill('@'); a[0].get_data("Tarbo C++", 1001, 250.95); a[1].get_data("C primer", 905, 95.7); a[2].get_data("algorithm", 1111, 120.5); a[3].get_d

7.Write a program to read a list containing item name, item code and cost interactively and produce a three-column output as shown below. NAME CODE COST Turbo C++ 1001 250.95 C Primer 905 95.70 ------------- ------- ---------- ------------- ------- ---------- Note that the name and code are left-justified and the cost is right-justified with a precision of two digits. Trailing zeros are shown.

 /*J_4_P_8 Write a program to read a list containing item name, item code and cost interactively and produce a three-column output as shown below. NAME                CODE      COST ======================= Turbo C++        1001          250.95 C Primer              905           95.70 ------------- ------- ----------------- ------------- ------- ----------------- Note that the name and code are left-justified and the cost is right-justified with a precision of two digits. Trailing zeros are shown. */ #include <iostream.h> #include <iomanip.h> #include <string.h> #include <conio.h> class item {         char name[40];         int code;         float cost; public:         void get_data(char *n, int c, float co)         {                 strcpy(name, n); code = c; cost = co; } void display(); }; void item::display() { cout.precision(2); cout.setf(ios::fixed,ios::floatfield); cout.setf(ios::showpoint); cout.setf(ios::left, ios::adjustfield); cout

1.Consider an example of media shop which sells cds and cassettes. These two classes are inherited from the base class called media. The media class has data members such as title and price. The cd class has data member for storing no of movies and cassette class has data member as no of songs. Each class has member functions as read() and show(). In the base class these members have to be defined as virtual functions. Write a progfram, which models the class hierarchy, and processes objects of these classes using pointers to the bases class

 /*J_4_P_1 Consider an example of media shop which sells cds and cassettes. These two classes are inherited from the base class called media. The media class has data members such as title and price. The cd class has data member for storing no of movies and cassette class has data member as no of songs. Each class has member functions as read() and show(). In the base class these members have to be defined as virtual functions. Write a progfram, which models the class hierarchy, and processes objects of these classes using pointers to the bases class */ #include <iostream.h> #include <conio.h> class media {     char title[10];     int price;     public:        virtual void read() {     cout << "Enter Title of Media : ";     cin.getline(title, 10);     cout << "Enter Media Price : ";     cin >> price; }        virtual void show() {     cout << "\nTitle of Media is " << title << endl;     cout

7.Write a program that accepts any file name from the command line and copies the contents of the file to a new temporary file.

 /*J_5_P_7 Write a program that accepts any file name from the command line and copies the contents of the file to a new temporary file. */ #include <iostream.h> #include <conio.h> #include <fstream.h> void main(int argc[], char *argv[]) { ifstream obj1; ofstream obj2; char ch; clrscr(); obj1.open(argv[1]); obj2.open("temp.txt"); while(obj1.eof() == 0) {        obj1 >> ch;        obj2 << ch; } cout << "File copy...";   obj1.close();   obj2.close();   getch(); }

6.Write a C++ program, which accepts any filename from command line and displays total no. of characters, total no of words and total no of lines in that file.

 /*J_5_P_6 Write a C++ program, which accepts any filename from command line and displays total no. of characters, total no of words and total no of lines in that file. */ #include<iostream> #include<fstream> #include<string.h> #include<cstdlib> using namespace std; int main() {      int noc=0,now=0,nol=0;      FILE *fr;      char ch;           fr=fopen("abc.txt","r");           while(ch!=EOF)      {           if(ch!=' ' && ch!='\n')                noc++;           if(ch==' ')                now++;           if(ch=='\n')           {                nol++;                now++;           }           ch=fgetc(fr);      }      fclose(fr);      cout<<" -------------------------------------";      cout<<"\n Total No. of Characters  : "<<noc;      cout<<"\n Total No. of Words       : "<<now;      cout<<"\n Total No. of Lines       : "&

5. Write a program to enter 25 characters from the user. Check all the characters one by one, if it is a digit store it in digit.txt file, if it is a special symbol store it in special.txt file, if it is an alphabet than store it in alphabet.txt file. Use command line arguments

/*J_5_P_5 Write a program to enter 25 characters from the user. Check all the characters one by one, if it is a digit store it in digit.txt file, if it is a special symbol store it in special.txt file, if it is an alphabet than store it in alphabet.txt file. Use command line arguments */ #include <iostream.h> #include <conio.h> #include <fstream.h> void main(int argc[], char *argv[]) { ofstream obj1, obj2, obj3; obj1.open(argv[1]); obj2.open(argv[3]); obj3.open(argv[5]); clrscr(); char ch[25]; for (int i = 0; i < 15; i++) { cout << "Enter any digit, chracter and symbol:"; cin >> ch[i]; if (ch[i] >= 48 && ch[i] <= 57) obj1 << ch[i] << endl; else if ((ch[i] >= 65 && ch[i] <= 90) || (ch[i] >= 97 && ch[i] <= 122)) obj2 << ch[i] << endl; else obj3 << ch[i] << endl; } getch(); }

4.Take 10 numbers from the user, check all the number one by one, if it is an odd number, store it in odd.txt file, if it is an even number, store it in even.txt file. Use command line arguments.

/* j_5_P_4 Take 10 numbers from the user, check all the number one by one, if it is an odd number, store it in odd.txt file, if it is an even number, store it in even.txt file. Use command line arguments. */ #include <iostream.h> #include <conio.h> #include <fstream.h> void main(int argc[], char *argv[]) { ofstream obj1; ofstream obj2; int no; clrscr(); obj1.open(argv[1]); obj2.open(argv[3]); do { cout << "Enter Any no: "; cin >> no; if( no % 2 == 1 ) { obj1 << no << endl; } else { obj2 << no << endl; } } while(no != 0); obj1.close(); obj2.close(); getch(); }

1.Write a program of writing binary file. Take two arrays 1) Roll no. of 10 students 2) Marks of 10 students Write information of both the arrays in one file and again read it form the file.

 /* J_5_P_1 Write a program of writing binary file. Take two arrays 1) Roll no. of 10 students 2) Marks of 10 students Write information of both the arrays in one file and again read it form the file.   */ #include <iostream.h> #include <conio.h> #include <fstream.h> void main() { clrscr(); ofstream out("student.txt"); int rno[10]; int mark[10]; for (int i = 1; i < 10; i++) { cout << "Enter student Roll No. : "; cin >> rno[i]; out << rno[i]; cout << "Enter student Marks. : "; cin >> mark[i]; out << mark[i]; } out.close(); ifstream in("student.txt"); cout << "Roll No\t Marks\n"; for (i = 1; i < 10; i++) { cout << rno[i] <<"\t  " << mark[i] << endl; } in.close(); getch(); }