Q:

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

belongs to collection: C++ programs on various topics

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to write and read an object in/from a ... >>
<< C++ - Read Characters from One file and Write them...