0% found this document useful (0 votes)
15 views11 pages

Programming Techniques

Some questions that might help you improve your programming.

Uploaded by

5124rabia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

Programming Techniques

Some questions that might help you improve your programming.

Uploaded by

5124rabia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Assignment No.

Course Name: Programming Techniques


Submitted by: Rabia Sarfraz
Roll No.: 2330-0070
Class Section: FA23/BS(SE)
Submitted to: Sir Yasir
DATE: 6th June, 24

“On my honor, as student at Sir Syed CASE Institute Islamabad, I have neither given nor
received unauthorized assistance on this academic work.”
Task No. 1:
Basic File Operations:
Create a C++ program that reads data from a text file named
"input.txt" and writes
it to another file named "output.txt". Ensure that the program
handles the file
opening and closing operations properly.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile("input.txt"); // Open input file for reading
ofstream outputFile("output.txt"); // Open output file for writing
// Check if input file was opened successfully
if (!inputFile.is_open())
{
cerr << "Failed to open input.txt" << std::endl;
return 1;
}
// Check if output file was opened successfully
if (!outputFile.is_open())
{
cerr << "Failed to open output.txt" << std::endl;
return 1;
}
string line;
// Read from input file line by line and write to output file
while (getline(inputFile, line))
{
outputFile << line << endl;
}
// Close the files
inputFile.close();
outputFile.close();
cout << "File has been copied successfully." << endl;
return 0;
}
Task no. 2:
File Appending:
Write a C++ program to open a file named "log.txt" in append mode.
Allow the
user to input log messages and append each message to the file along
with a
timestamp. Make sure to handle errors gracefully.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
string getCurrentTimestamp()
{
time_t now = time(0);
tm *ltm = localtime(&now);

char buffer[20];
snprintf(buffer, sizeof(buffer), "%04d-%02d-%02d %02d:%02d:%02d",
ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday,
ltm->tm_hour, ltm->tm_min, ltm->tm_sec);

return string(buffer);
}

int main()
{
ofstream logFile("log.txt",ios::app); // Open log.txt in append mode
// Check if log file was opened successfully
if (!logFile.is_open()) {
cerr << "Failed to open log.txt" << endl;
return 1;
}
string logMessage;
cout << "Enter log message (type 'exit' to quit): ";
while (getline(cin, logMessage)) {
if (logMessage == "exit") {
break;
}
string timestamp = getCurrentTimestamp();
logFile << "[" << timestamp << "] " << logMessage <<endl;
cout << "Enter log message (type 'exit' to quit): ";
}
// Close the log file
logFile.close();
cout << "Log file has been updated successfully." <<endl;
return 0;
}

Task No. 3:
Record Management:
 Develop a C++ program to manage a student database using a file.
The program
should allow users to add new student records, display all records,
and search for
a student by their roll number. The data should be stored in a file
named
"students.dat.”
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

struct Student {
int rollNumber;
char name[50];
int age;
char course[50];
};

void addStudent();
void displayAllStudents();
void searchStudentByRollNumber();

int main() {
int choice;

do {
cout << "Student Database Management System" << endl;
cout << "1. Add New Student" << endl;
cout << "2. Display All Students" << endl;
cout << "3. Search Student by Roll Number" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
addStudent();
break;
case 2:
displayAllStudents();
break;
case 3:
searchStudentByRollNumber();
break;
case 4:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 4);

return 0;
}

void addStudent() {
Student student;

cout << "Enter Roll Number: ";


cin >> student.rollNumber;
cin.ignore(); // Clear the newline character from the buffer

cout << "Enter Name: ";


cin.getline(student.name, 50);

cout << "Enter Age: ";


cin >> student.age;
cin.ignore(); // Clear the newline character from the buffer

cout << "Enter Course: ";


cin.getline(student.course, 50);

ofstream file("students.dat", ios::binary | ios::app);


if (!file) {
cerr << "Failed to open students.dat" << endl;
return;
}

file.write(reinterpret_cast<char*>(&student), sizeof(student));
file.close();

cout << "Student record added successfully!" << endl;


}

void displayAllStudents() {
Student student;
ifstream file("students.dat", ios::binary);

if (!file) {
cerr << "Failed to open students.dat" << endl;
return;
}

while (file.read(reinterpret_cast<char*>(&student), sizeof(student)))


{
cout << "Roll Number: " << student.rollNumber << endl;
cout << "Name: " << student.name << endl;
cout << "Age: " << student.age << endl;
cout << "Course: " << student.course << endl;
cout << "-----------------------" << endl;
}

file.close();
}

void searchStudentByRollNumber() {
int rollNumber;
Student student;
bool found = false;

cout << "Enter Roll Number to search: ";


cin >> rollNumber;

ifstream file("students.dat", ios::binary);


if (!file) {
cerr << "Failed to open students.dat" << endl;
return;
}

while (file.read(reinterpret_cast<char*>(&student), sizeof(student)))


{
if (student.rollNumber == rollNumber) {
cout << "Roll Number: " << student.rollNumber << endl;
cout << "Name: " << student.name << endl;
cout << "Age: " << student.age << endl;
cout << "Course: " << student.course << endl;
found = true;
break;
}
}

if (!found) {
cout << "Student with Roll Number " << rollNumber << " not
found." << endl;
}

file.close();
}
Task No. 4:
Code:
...

THE END!

You might also like