Problem with 2 text file contents string compare

Trying to compare the contents of 2 files, and if they match return(0); and exit, but if they dont match to then run emailme.exe which runs a keyboard/mouse macro written using jitbit macro recorder to e-mail me that an automated task failed to complete.

The automated task is a batch file and at the end of the batch file I have:
echo. %date%>Last_Run.txt

When testing this program I am getting:

String Data Read in from files shown below:

03/21/2013
ê$>
Press any key to continue . . .


where I would expect to see:

String Data Read in from files shown below:

Thu 03/21/2013
Thu 03/21/2013
Press any key to continue . . .


since both text files ( Last_Run.txt and Todays_Date.txt ) contain
Thu 03/21/2013

Source Code I have is:

#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>
using namespace std;

int main ()
{

char string1[20];
char string2[20];
// Write todays date to file for comparison with same format of that of batch log
system("@echo. %date%>Todays_Date.txt");

char *inname = "Todays_Date.txt";
ifstream infile(inname);

if (!infile) {
cout << "There was a problem opening file "
<< inname
<< " for reading."
<< endl;
return 0;
}
while (infile >> string1) {

}


char *inname2 = "Last_Run.txt";
ifstream infile2(inname2);

if (!infile2) {
cout << "There was a problem opening file "
<< inname2
<< " for reading."
<< endl;
return 0;
}
while (infile >> string2) {

}
//NOTE: Display string data - remove from program when correct
cout<<"String Data Read in from files shown below: \n";
cout<<"\n"<<string1<<"\n"<<string2<<"\n";
system("pause");
//NOTE: Remove everything between here and above note once correct


if(strcmp(string1,string2)==0){
// ==0 FALSE
cout<<"Mismatch\n";
system("pause");
//system("emailme.exe");
}
else {
// ==1 TRUE
cout<<"Matching Date\n";
system("pause");
}
return 0;
}


Haven't performed a string compare in years, so had to reference online how to do it, and now I am stumped as to why reading in from these files the string data being passed to the char variables is messing up, and causing string compare to malfunction due to data read in not matching.
if(strcmp(string1,string2)==0)
you might try changing it to
if(strcmp(string1.c_str(), string2.c_str())==0)
Why ? He does not use std::strings anywhere.
Why don't you use std::string and you could then use the compare(...) function of the std::string?
modoran is right ;) i just looked for strcmp without checking whats above it.. it 's probably because of no code tags ;/
@OP What are you trying to do ? Writing current date to a file using system and shell %date% environment, reading it back and comparing it with other file contents it not a logical thing to do.

Why not just get the date directly from <ctime> header standard functions ?
http://www.cplusplus.com/reference/ctime/
Topic archived. No new replies allowed.