You are on page 1of 7

File handling

#include <iostream>
#include <fstream>
using namespace std;

int main() {
// Create and open a text file
ofstream MyFile("filename.txt");
// Write to the file
MyFile << "Files can be tricky, but it is fun enough!";

// Close the file


MyFile.close();

// Create a text string, which is used to


output the text file string myText;

// Read from the text file


ifstream MyReadFile("filename.txt");

// Use a while loop together with the getline() function


to read the file line by line

while (getline (MyReadFile, myText)) {


// Output the text from the file
cout << myText;
}

// Close the file

MyReadFile.close();

#include <iostream>
#include <fstream>
#define FILE_NAME "emp.dat50"

using namespace std;

//class employee declaration


class Employee {
private :
int empID;
char empName[100] ;
char designation[100];
int ddj,mmj,yyj;
int ddb,mmb,yyb;
public :
//function to read employee details
void readEmployee(){
cout<<"EMPLOYEE DETAILS"<<endl;
cout<<"ENTER EMPLOYEE ID : " ;
cin>>empID;
cin.ignore(1);
cout<<"ENTER NAME OF THE EMPLOYEE : ";
cin.getline(empName,100);

cout<<"ENTER DESIGNATION : ";


cin.getline(designation,100);

cout<<"ENTER DATE OF JOIN:"<<endl;


cout<<"DATE : "; cin>>ddj;
cout<<"MONTH: "; cin>>mmj;
cout<<"YEAR : "; cin>>yyj;

cout<<"ENTER DATE OF BIRTH:"<<endl;


cout<<"DATE : "; cin>>ddb;
cout<<"MONTH: "; cin>>mmb;
cout<<"YEAR : "; cin>>yyb;
}
//function to write employee details
void displayEmployee(){
cout<<"EMPLOYEE ID: "<<empID<<endl
<<"EMPLOYEE NAME: "<<empName<<endl
<<"DESIGNATION: "<<designation<<endl
<<"DATE OF JOIN: "<<ddj<<"/"<<mmj<<"/"<<yyj<<endl
<<"DATE OF BIRTH:
"<<ddb<<"/"<<mmb<<"/"<<yyb<<endl; }
};

int main(){

//object of Employee class


Employee emp;
//read employee details
emp.readEmployee();

//write object into the file


fstream file;
file.open(FILE_NAME,ios::out|ios::binary);
if(!file){
cout<<"Error in creating file...\n";
return -1;
}
file.write((char*)&emp,sizeof(emp));
file.close();
cout<<"Date saved into file the file.\n";

//open file again


file.open(FILE_NAME,ios::in|ios::binary);
if(!file){
cout<<"Error in opening file...\n";
return -1;
}

if(file.read((char*)&emp,sizeof(emp))){
cout<<endl<<endl;
cout<<"Data extracted from file..\n";
//print the object
emp.displayEmployee();
}
else{
cout<<"Error in reading data from file...\n";
return -1;
}

file.close();
return 0;
}

Output Screen:
#include<iostream>
usingnamespace std;
int main(){
char greeting[6]={'H', 'e', '1', '1', 'o', '\0'};
cout<<"Greeting message: ";
cout << greeting << endl;
return;
}

Cstring
#include<cstring>
usingnamespace std;
int main (){
char str1[10]="Hello";
char str2[10]="World";
char str3[10];
int len ;
// copy str1 into str3
strcpy( str3, str1);
cout<<"strcpy( str3, str1): "<< str3 <<< endl;
// concatenates str1 and str2
strcat( str1, str2);
cout<<"strcat( str1, str2): "<< str1 <<< endl;
// total lenghth of str1 after concatenation
len = strlen(str1);
cout<<"strlen(str1): "<< len << endl;
return;
}

#include<string>
usingnamespace std;
int main(){
string str1 ="Hello";
string str2 = "World";
string str3;
int len;
// copy str1 into str3
str3 = str1;
cout<<"str3: "<< str3 <<<< endl;
// concatenates str1 and str2
str3 = strl + str2;
cout <<"str1 + str2 "<< str3 << endl;
// total lenghth of str3 after concatenation
len str3.size();
=
cout<<"str3.size():
"<<< len << endl;
return;
}
When the above code is compiled and executed, it produces result something as
follows:
str3 Hello
str1 + str2: Helloworld
str3.size(): 10

#include <iostream>
#include <cstring>

int main() {
const int MAX_SIZE = 100;

// Concatenating using cstring


char firstCString[MAX_SIZE];
char secondCString[MAX_SIZE];

std::cout << "Enter the first string: ";


std::cin.getline(firstCString, MAX_SIZE);

std::cout << "Enter the second string: ";


std::cin.getline(secondCString, MAX_SIZE);

// Concatenating without using any function


int i = 0;
while (firstCString[i] != '\0') {
++i;
}

int j = 0;
while (secondCString[j] != '\0') {
firstCString[i] = secondCString[j];
++i;
++j;
}

firstCString[i] = '\0';

std::cout << "Concatenated String (using cstring): " << firstCString << std::endl;

return 0;
}

You might also like