Home » Code Snippets » C/C++ Code Snippets

C++ program to write and read time in/from binary file using fstream

In this program we will learn how to write and read time in/from binary file using fstream in c++ programming language?

There will be two functions

  • writeTime() - that will write time into the file
  • readTime() - that will read time from the file

In this program, there are two things to be noticed, how time will be formatted into a string (using sprintf) and how time values will be extracted from the string (using sscanf).

Program to write, read time in,from binary file in C++

#include <iostream>
#include <fstream>
#include <iomanip> //for setfill() and setw()

using namespace std;

#define FILE_NAME "time.dat"

//function to write time into the file
void writeTime(int h, int m, int s){	
	
	char str[10];
	
	fstream file;
	file.open(FILE_NAME, ios::out|ios::binary);

	if(!file){
		cout<<"Error in creating file!!!"<<endl;
		return;
	}

	//make string to write
	sprintf(str,"%02d:%02d:%02d",h,m,s);

	//write into file
	file.write(str,sizeof(str));
	cout<<"Time "<<str<<" has been written into file."<<endl;
	
	//close the file
	file.close();
	
}

//function to read time from the file
void readTime(int *h,int *m, int *s){
	
	char str[10];
	int inH,inM,inS;

	fstream finC;
	finC.open(FILE_NAME,ios::in|ios::binary);
	if(!finC){
		cout<<"Error in file opening..."<<endl;
		return;
	}
	if(finC.read((char*)str,sizeof(str))){
		//extract time values from the file
		sscanf(str,"%02d:%02d:%02d",&inH,&inM,&inS);
		//assign time into variables, which are passing in function
		*h=inH;
		*m=inM;
		*s=inS;
	}
	finC.close();	
}

int main(){
	int m,h,s;
	
	cout<<"Enter time:\n";
	cout<<"Enter hour: "; 	cin>>h;
	cout<<"Enter minute: "; cin>>m;
	cout<<"Enter second: "; cin>>s;

	//write time into file
	writeTime(h,m,s);
	
	//now, reset the variables
	h=m=s=0;
	
	//read time from the file 
	readTime(&h,&m,&s);
	
	//print the time
	cout<<"The time is "<<setw(2)<<setfill('0')<<h<<":"<<setw(2)<<setfill('0')<<m<<":"<<setw(2)<<setfill('0')<<s<<endl;	
	
	return 0;
}

Output

Enter time: 
Enter hour: 10
Enter minute: 15
Enter second: 5 
Time 10:15:05 has been written into file. 
The time is 10:15:05


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.