#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <sstream>
#using <System.dll>
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>
#include <cstring>
#include <time.h>
using namespace System;
using namespace std;
using namespace msclr::interop;
using namespace System::IO::Ports;
void *write();
void *attendance();
bool empty (ifstream& myfile);
string line;
int main()
{
SerialPort^ mySerialPort = gcnew SerialPort("COM4");
int m=0;
mySerialPort->BaudRate = 2400;
mySerialPort->Parity = Parity::None;
mySerialPort->StopBits = StopBits::One;
mySerialPort->DataBits = 8;
mySerialPort->Open();
while(m==0)
{
String^ message = mySerialPort->ReadLine();
line=marshal_as<string>(message);
if (line.length()>20)
{
write();
line.clear();
}
else
{
attendance();
line.clear();
}
}
mySerialPort->Close();
return 0;
}
void *attendance()
{
char *cstr;
cstr = new char [line.size()+1];
strcpy (cstr,line.c_str());
string Symbol[2]={"",""};
char *station=0;
time_t now;
struct tm *current;
now = time(0);
current = localtime(&now);
char *dt=ctime(&now);
int hour=current->tm_hour;
int min=current->tm_min;
int sec=current->tm_sec;
int year=1900+current->tm_year;
int month=1+current->tm_mon;
int day=current->tm_mday;
station=strtok(cstr,"\t \n");
int i=0;
while(station!=NULL)
{
Symbol[i]=station;
i++;
station=strtok(NULL,"\t \n");
}
ifstream myfile ("d:/attendance.txt");
if (myfile.is_open())
{
bool check = empty(myfile);
if(check) // if true file is empty
{
ofstream myfile("d:/attendance.txt");
cout<<hour<<":"<<min<<"\t"<<" "<<month<<"-"<<day<<"-"<<year<<"\t"<<Symbol[0]<<endl;
myfile<<hour<<":"<<min<<"\t"<<" "<<month<<"-"<<day<<"-"<<year<<"\t"<<Symbol[0];
myfile.close();
return 0; // return;
}// end of if check, file is empty
else
{
ofstream outfile("d:/attendancetemp.txt");
// ifstream myfile("d:/output.txt");
while(!myfile.eof())// read all entries and add to temp file
{
getline(myfile,line);
outfile<<line<<endl;
}
cout<<hour<<":"<<min<<"\t"<<" "<<month<<"-"<<day<<"-"<<year<<"\t"<<Symbol[0]<<endl;
outfile<<hour<<":"<<min<<"\t"<<" "<<month<<"-"<<day<<"-"<<year<<"\t"<<Symbol[0];
outfile.close();
myfile.close();
ifstream myfile("d:/attendance.txt");
myfile.close();
remove("d:/attendance.txt");
rename("d:/attendancetemp.txt", "d:/attendance.txt");
}
}
return 0;
}
void *write()
{
char *cstr;
cstr = new char [line.size()+1];
strcpy (cstr,line.c_str());
string Symbol[3]={"","",""};
char *station=0;
char *status=0;
char *tag=0;
time_t now;
struct tm *current;
now = time(0);
current = localtime(&now);
char *dt=ctime(&now);
int hour=current->tm_hour;
int min=current->tm_min;
int sec=current->tm_sec;
int year=1900+current->tm_year;
int month=1+current->tm_mon;
int day=current->tm_mday;
station=strtok(cstr,"\t \n");
int i=0;
while(station!=NULL)
{
Symbol[i]=station;
i++;
station=strtok(NULL,"\t \n");
}
ifstream myfile ("d:/output.txt");
if (myfile.is_open())
{
bool check = empty(myfile);
if(check) // if true file is empty
{
ofstream myfile("d:/output.txt");
cout<<hour<<":"<<min<<"\t"<<" "<<month<<"-"<<day<<"-"<<year<<"\t"<<Symbol[0]<<"\t"<<Symbol[1]<<"\t"<<Symbol[2]<<endl;
myfile<<hour<<":"<<min<<"\t"<<" "<<month<<"-"<<day<<"-"<<year<<"\t"<<Symbol[0]<<"\t"<<Symbol[1]<<"\t"<<Symbol[2];
myfile.close();
return 0; // return;
}// end of if check, file is empty
else
{
ofstream outfile("d:/outfile.txt");
// ifstream myfile("d:/output.txt");
while(!myfile.eof())// read all entries and add to temp file
{
getline(myfile,line);
outfile<<line<<endl;
}
cout<<hour<<":"<<min<<"\t"<<" "<<month<<"-"<<day<<"-"<<year<<"\t"<<Symbol[0]<<"\t"<<Symbol[1]<<"\t"<<Symbol[2]<<endl;
outfile<<hour<<":"<<min<<"\t"<<" "<<month<<"-"<<day<<"-"<<year<<"\t"<<Symbol[0]<<"\t"<<Symbol[1]<<"\t"<<Symbol[2];
outfile.close();
myfile.close();
ifstream myfile("d:/output.txt");
myfile.close();
remove("d:/output.txt");
rename("d:/outfile.txt", "d:/output.txt");
}
}
return 0;
}
bool empty(std::ifstream& myfile)
{
return myfile.peek() == std::ifstream::traits_type::eof();
}
Login