I'm don't fully understand how im supposed to compare the two different files, i got the program to read in the txt file and give an error if its not in the folder but then i got stuck heres the prolem
Write a main function that performs the following
operations:
a.) Prompts the user to input the names of 3 files: two input files and
an output file.
b.) Reads in the two input files line by line and compares the two
lines.
c.) For each line in the inputs, the output should have the line number,
a colon (':'), and either "OK" if the lines match or "DIFF" if the lines
are different.
#include "std_lib_facilities_3.h"
int main()
{
string file1;
string file2;
string out_file;
{
cout<<"Please enter the name of file 1\n";
getline(cin,file1);
ifstream ist(file1.c_str());
if(!ist) error("can't open input file ",file1);
}
{
cout<<"Please enter the name of file 2\n";
getline(cin,file2);
ifstream ist(file2.c_str());
if(!ist) error("can't open input file ",file2);
}
{
cout<<"Please enter the name of ouput file\n";
getline(cin,out_file);
ofstream ost(out_file.c_str());
if(!ost) error("can't open ouput file ",out_file);
ost<<"1:"<< <<"\n";
ost<<"2:"<< <<"\n";
ost<<"3:"<< <<"\n";
ost<<"4:"<< <<"\n";
}
return 0;
}
It depends on how you read the file. if you use ifstream.getline(), the file stream stops reading at the first '\n' character, or the end of the stream. it then moves the file pointer.
While file 1 has text, read each line until end of file.
Push each line (std::string) into a vector of strings... vector<string>
Close file 1
While file 2 has text, read each line until end of file.
Push each line (std::string) into a vector of strings... vector<string>
Close file 2
for each element in the vectors, until the end of the smallest vector, compare each string for that index... if(vector1[index] == vector2[index]) Print out depending on that comparison. (std output or file)
If the vectors are not the same length(1 file is larger than the other), then the remaining lines automatically fall into the "DIFF" catagory.
You don't need vector<>s for this homework. You do, however, need to create those file streams outside of local blocks. (Try getting rid of lines 9, 14, 16, 21, etc. Notice that you will have to rename the ifstreams: something like ist1 and ist2 maybe?)
You will also need to use a loop; do not assume that both files have exactly four lines of text.
I purposely only showed the comparison. You need a loop of some sort. Using either iterators or an indices. Right? Because you need to iterate over each element, comparing each one (index version)if(vector1[index1]==vector2[index1]) or (iterator version)if(*itr1==*itr2), then depending on that comparison you take an action. By itself, the comparison is useless, as you can see from your code.
for example. Lets say I have two variable and I want to know which one is larger.
1 2 3 4 5
int a(5), b(10);
if(a < b) //Here is the comparison, same as the vector1[index]==vector2[index]
cout << "A is less than B" << endl; //action if true
else
cout << "A is not less than B" << endl; //action if false
You need to implement the action for your assignment based on the DIFF/SAME or whatever it is.
alright updated and its working to a certain it only reads up to shortest txt file line and still didn't count the abc and abc as ok but saw them as different
test is just to show which if statement it goes into
...and this is why I hate responding to threads like this...
because the OP, knowing very little, will often follow advice that is against his best interests, presented as a simple, follow the example solution without all the relevant caveats.
@loredo
You are wasting your time and energy playing with vector<>s. What started as a very simple homework has been deviated into a much larger project than it need be, with you finding yourself much more confused than at the start. If you turn that code in, you will fail.
Get rid of the *&#! vectors and do exactly what your homework asks:
1) open three files: two for input and one for output.
2) while you can read lines (one from each input file):
3) compare the two lines and write a response to the output file
Your homework is not to rewrite diff(1) or any other very advanced pattern matching program.