Docsity

Computer Programming Lab Report: File Handling and Data Processing in C++, Lecture notes of Computer Architecture and Organization

Partial preview of the text

Download Computer Programming Lab Report: File Handling and Data Processing in C++ and more Lecture notes Computer Architecture and Organization in PDF only on Docsity!

UNIVERSITY OF ENGINEERING & TECHNOLOGY, TAXILA.

COMPUTER ENGINEERING DEPARTMENT.

LAB REPORT

Assignment

COMPUTER PROGRAMMING (CP)

Submitted To

Sir Shahryar Khan

Submitted By

Chaudary Bilal Ahmed

21 - CP- 79

Alpha

TASK No.

Write a program that will input the record of 5 students in a file named “Personal_Info.txt”. The information inserted in the file is: Name, Gender, Age, Address and Cell Phone.

  • Input: #include #include using namespace std; int main() { ofstream students; string s[10]; students.open("Personal_info.txt"); students << "\t\t Data of Students" << endl; students << "Name Gender Age Address Cell Phone" << endl; students << "Bilal Male 18 ISL 009988779" << endl; students << "Ali Male 19 MUL 8948478965" << endl; students << "Umer Male 14 ISL 8965421561" << endl; students << "Yousaf Male 20 ISL 5446161618" << endl; students << "Akbar Female 25 LAH 5144616845" << endl; students.close(); }
  • Output:

cin >> quiz3[i]; if (quiz1[i] + quiz2[i] + quiz3[i] <= 100 && quiz1[i] + quiz2[i] + quiz3[i] > 80) { grade = 'A'; } else if (quiz1[i] + quiz2[i] + quiz3[i] <= 80 && quiz1[i] + quiz2[i] + quiz3[i] > 60) { grade = 'B'; } else { grade = 'F'; } f << n[i] << " " << ad[i] << " " << quiz1[i] << " " << quiz2[i] << " " << quiz3[i] << " " << quiz1[i] + quiz2[i] + quiz3[i]<<" " << grade << endl; } f.close(); return 0; }

  • Output:

TASK No.

Write a program that read the record of the students stored in a data file in the program in

Question No 02 and print the record on the screen.

  • Input: #include #include #include using namespace std; int main() { fstream task; string a; task.open("Data.txt", ios::in);

TASK No.

**Create a text file named as “num.txt” in any text editor. Write three numbers in each line as shown below. 66 33 16 13 29 12


Write a program that reads three numbers from each line, calculate the sum and store the result in a file “sum.txt” as shown below. (use tellp() and seekp() functions to get pointer position) 66 33 16 115 13 29 12 054 -- -- -- ----**

  • Input: #include #include using namespace std; int main() { fstream in, out; int i, f, g, x, y, z; in.open("num.txt", ios::in); out.open("sum.txt", ios::out); in >> i >> f >> g >> x >> y >> f; out << i << " " << f << " " << g << "= " << i + f + g << endl << x << " " << y << " " << f << "= " << x + y + f << endl; return 0; }
  • Output:

TASK No.

Write a program that reads three numbers from the file specified by the user, sums the numbers, and writes the sum to another file specified by the user.

  • Input: #include #include #include using namespace std; int main() { ofstream task("task1.txt"); ofstream part("part.txt"); task << "10 5 20" << endl; ifstream reader("task1.txt"); int a; int b; int c; if (reader.is_open()) { reader >> a; reader >> b; reader >> c; cout << "Sum of numbers : " << a + b + c << endl; } part << "The sum of numbers 10 5 20 is :" << a + b + c << endl; task.close(); part.close(); reader.close(); }
  • Output:

cout << "The Largest number is :" << l << endl; cout << endl << endl; if (i < j && i < k && i < l) cout << "The Smallest number is : " << i << endl; if (j < i && j < k && j < l) cout << "The Smallest number is : " << j << endl; if (k < i && k < j && k < l) cout << "The Smallest number is : " << k << endl; if (l < i && l < j && l < k) cout << "The Smallest number is : " << l << endl; } writer.close(); reader.close(); }

  • Output:

TASK No.

Write a program that takes input both from an input file and from the keyboard. The input file contains the text of an official letter, except that the name of the recipient is indicated by the three characters #N#. The program asks the user for a name and then writes the letter to a second file but with the three letters #N# replaced by the name. The three-letter string #N# will occur exactly once in the letter.

  • Input: #include #include #include using namespace std; int main() { ifstream temp; ofstream final; string s, s1; s = "Bilal"; temp.open("lets.txt"); final.open("leto.txt"); while (getline(temp, s1)) { final << s1 << endl; } final.seekp(-5, ios::end); final << s; }

b1.open("T81.txt"); a1 >> s >> a >> b >> c >> d >> e; b1.seekp(ios::end); b1 << endl; sum = a + b + c + d + e; b1 << "Total Sum is " << sum << endl; if (sum <= 100 && sum > 80) { b1 << "grade is A" << endl; } if (sum <= 80 && sum > 50) { b1 << "grade is B" << endl; } if (sum <= 50 && sum > 33) { b1 << "grade is C" << endl; } if(sum<=33) b1 << "grade is F"<<endl; }

  • Output: