#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
bool Found;
string str;
ifstream file ("example.dat",ios::in | ios::binary);
string str2;
ifstream file2 ("example2.dat",ios::in | ios::binary);
while(!file.eof() || Found) // The "Is found true?" check is placed at both while statements to ensure that they both end as soon as a match is found.
{
getline(file, str, ' '); //Get the first string inside example.dat and store it.
while (!file2.eof() || Found) //This is one way to end our loop early, when a match is found it stops the loop
{
getline(file2, str2, ' '); //Get the first string inside example2.dat and store it
if (str == str2)
{
Found = true; //This is our bool to check if a match was found.
}
str2.clear(); // erase str2 so we can check the next string in example2.dat
}
str.clear(); // erase str so we can check the next string in example.dat
}
if (Found)
{
cout<<"This order is valid."<<endl;
}
else
{
cout<<"This order is invalid."<<endl;
}
system("pause");
return 0;
}
This program compares two files. Example.dat is compared to Example2.dat and if any of the values in Example.dat do not match the values in Example2.dat, the output on the screen will read "This order is invalid." If everything matches up, the output will read "This order is valid." Recently, someone has been very helpful in helping me tweak my program and I believe I have my code almost all correct, but after I compile it, I run it and there is no output. Just a blank .exe box. Anyone know what the problem could be?
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
bool Found=true;
string str;
ifstream file ("example.dat",ios::in | ios::binary);
string str2;
ifstream file2 ("example2.dat",ios::in | ios::binary);
while(!file.eof() && Found) // The "Is found true?" check is placed at both while statements to ensure that they both end as soon as a match is found.
{
// From your post I assume the entries in the files are separated by spaces.
getline(file, str, ' '); //Get the first string inside example.dat and store it.
while (!file2.eof() && Found) //This is one way to end our loop early, when a match is found it stops the loop
{
getline(file2, str2, ' '); //Get the first string inside example2.dat and store it
if (str == str2)
{
Found = true; //This is our bool to check if a match was found.
}
str2.clear(); // erase str2 so we can check the next string in example2.dat
}
str.clear(); // erase str so we can check the next string in example.dat
}
if (Found)
{
cout<<"This order is valid."<<endl;
}
else
{
cout<<"This order is invalid."<<endl;
}
system("pause");
return 0;
}
As I said earlier, I'm comparing these two files. Example1 can change to whatever values a user saves, but Example2 stays the same. The goal of the program is to determine whether or not the file as a whole is valid (i.e. whatever values saved in Example.dat match those in Example2.dat)
As per my understanding Example2.dat file contains all the template data and you need to compare the each and every data of Example1 file into Example2.dat file.
for this you require two counters one to get the number of values in Example1.dat file and save it
and another one will keep on increanig as you find each string matcihng.
if (str == str2)
{
Found = true; //This is our bool to check if a match was found.
statusflag++ // like this
}
compare the count of both the counters it must match for exact matching of files.
Thanks for the response. I guess I'm somewhat confused on this process. After you use the second counter which counts as you find each string matching, you compare that to the number of values in the first file? And if they equal each other, this would thus mean that your files equal each other and would print out on screen "This order is valid"?
If that's how you go about using a counter for each matching string, how would you use one to get the number of values in Example1.dat?
I already tried getline(file, str, ' '); statusflag++ but that didn't work at all. and then at the end of the file,
if (statusflag == statusflag2)
{
cout<<"This order is valid."<<endl;
}
else
{
cout<<"This order is invalid."<<endl;
}