Hello everyone

I have written a small c++ program where two files are compared line by line and in case the lines are different, the differing ones are put into a report.txt document.But its not working right!

The first few lines which are same are not put into the report.txt, but all the remaining ones, be it same or not are being copied!

The following is my code

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

using namespace std;


void main()
{
	ifstream Ifile1("D:\\file1.txt");
	ifstream Ifile2("D:\\file2.txt");
	ofstream out("D:\\Report.txt");
	char Line1[256],Line2[256];
	int check=0;

		
	system("pause");

	
	while (Ifile1.getline (Line1,256) && Ifile2.getline (Line2,256))
		{
		check=strcmp(Line1,Line2);
		if(check!=0)
			out<<Line2<<endl;
		
	}
	out.flush();
	out.close();
}

P.S. I wrote the program so that if the lines are differing only the lines from the second file are copied.

Please help with my problem! I need to get this done asap!

Thanks in advance

Recommended Answers

All 11 Replies

I copied and ran your code and it worked fine for me. On unix though. Btw main should have a return type of int and you should remove all the unnecessary headers and don't use System("pause") , use cin.get if you want to pause your program.

Use couts to print out each line as you read and try using a debugger.

I would start by switching from char arrays to std::string and from strcmp to .compare - http://programmingexamples.net/index.php?title=CPP/Strings/Compare
http://programmingexamples.net/index.php?title=CPP/IO/ReadingLines

Hello David,

I've switched from char arrays to string and also switched to .compare. But am getting the following errors!

Compiling...
cpp2.cpp
D:\Documents and Settings\e517943\My Documents\TEST\cpp2.cpp(41) : error C2039: 'getline' : is not a member of 'std'
D:\Documents and Settings\e517943\My Documents\TEST\cpp2.cpp(41) : error C2065: 'getline' : undeclared identifier
D:\Documents and Settings\e517943\My Documents\TEST\cpp2.cpp(41) : error C2039: 'getline' : is not a member of 'std'
D:\Documents and Settings\e517943\My Documents\TEST\cpp2.cpp(48) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or 
there is no acceptable conversion)
Error executing cl.exe.

cpp2.exe - 4 error(s), 0 warning(s)

Here is my code again if you would like to have a look

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



int main()
{
	ifstream Ifile1("D:\\file1_vss\\NP\\NP_DATA_LIB.CPP");
	ifstream Ifile2("D:\\file2_desktop\\NP\\np_data_lib.cpp");
	ofstream out("D:\\Report.txt");
	std::string Line1,Line2;
	int check=0;

		
	cout<<"Press any key to continue:";
	cin.get();

	
	while (std::getline(Ifile1,Line1) && std::getline(Ifile2,Line2))
		{
		   check=Line1.compare(Line2);
		   if(check!=0)
			out<<Line2<<endl;
			out.flush();
		
	          }
	
	out.close();
}

Plz help

Thank you

Hello David,

I've switched from char arrays to string and also switched to .compare. But am getting the following errors!

Compiling...
cpp2.cpp
D:\Documents and Settings\e517943\My Documents\TEST\cpp2.cpp(41) : error C2039: 'getline' : is not a member of 'std'
D:\Documents and Settings\e517943\My Documents\TEST\cpp2.cpp(41) : error C2065: 'getline' : undeclared identifier
D:\Documents and Settings\e517943\My Documents\TEST\cpp2.cpp(41) : error C2039: 'getline' : is not a member of 'std'
D:\Documents and Settings\e517943\My Documents\TEST\cpp2.cpp(48) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or 
there is no acceptable conversion)
Error executing cl.exe.

cpp2.exe - 4 error(s), 0 warning(s)

Here is my code again if you would like to have a look

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



int main()
{
	ifstream Ifile1("D:\\file1_vss\\NP\\NP_DATA_LIB.CPP");
	ifstream Ifile2("D:\\file2_desktop\\NP\\np_data_lib.cpp");
	ofstream out("D:\\Report.txt");
	std::string Line1,Line2;
	int check=0;

		
	cout<<"Press any key to continue:";
	cin.get();

	
	while (std::getline(Ifile1,Line1) && std::getline(Ifile2,Line2))
		{
		   check=Line1.compare(Line2);
		   if(check!=0)
			out<<Line2<<endl;
			out.flush();
		
	          }
	
	out.close();
}

Plz help

Thank you

Hey...I've figured out the errors...had to #include<string> thats all.

But I am still having the same problem! first few lines, which are not same are copied...after that every line is getting copied!

UPDATE:

Thanks for the replies!

I've executed the code step by step. The problem is that the blank spaces are getting skipped in file 1. Hence, it jumps a few lines ahead. Therefore, it always results in a mis-match.

Is there any fix ? A way to make getline read a blank space? BTW I dont know anything about parsing.


Thank u!

Can you post the input files here ? A small portion may be ? As far as i know getline reads the entire line along with the spaces if any. Do you mean you have a few blank lines in between? This was the input I used and it worked fine

file1.txt

line0
line2
line3
line4
This is the last line

file2.txt

line0 are you there
line2
line4
line3
This isthe last line

Report.txt

line0 are you there
line4
line3
This isthe last line

UPDATE:

Thanks for the replies!

I've executed the code step by step. The problem is that the blank spaces are getting skipped in file 1. Hence, it jumps a few lines ahead. Therefore, it always results in a mis-match.

Is there any fix ? A way to make getline read a blank space? BTW I dont know anything about parsing.


Thank u!

Hi plz ignore my earlier post

I've just had a look at the files I am comparing. The problem lies in the Lines itself. File 2 is an update of File 1.It has some lines inserted in between. Hence, as I am doing a line by line compare sequentially(Line 1 of file 1 to Line 1 of file 2 etc):'(, it results in mismatch of all lines from the point of insertion of new lines.

To illustrate

File 1:
Line1
Line2
Line3
Line4
Line5

File 2:
Line1
Line2
Line67
Line3
Line4
Line5

Hence, a mismatch results from "Line67" onwards. Is there any way to work around this problem?

Plz help

Thank you!
Sreekiran

Sreekiran,

As a small side note, please use real English words like "please" instead of "plz" - it helps keep Daniweb looking professional!

You need to have some logic to find out which lines are the 'extra' lines and can be ignored while comparing. for example say a line starting with 'line67' is noise and should not be compared. If on the other hand it is random then I can't imagine a workaround and the comparison makes no sense.

David:
Okay...sorry!

Agni:
Is there any way to have a pointer pointing to each line? Any way to implement it?

Also if I compare one line of file 1 to all lines of file2 and if it doesnt match with any, print out that line...will that work?


Thank you

Is there any way to have a pointer pointing to each line? Any way to implement it?

You could store the entire file as a vector of strings and then do whatever you want to do with them. Something like

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

int main()
{
	std::ifstream repFile("file1.txt");	
	
	std::vector<std::string> fileVector;	
	
	std::string repLine;
	while(getline(repFile,repLine))
	{
		fileVector.push_back(repLine);
	}

	for(int i=0;i<fileVector.size(); i++)
	{
		std::cout << fileVector[i] << std::endl;
	}

	std::cin.get();
}

Also if I compare one line of file 1 to all lines of file2 and if it doesnt match with any, print out that line...will that work?

That depends on what your program is expected to do. I can't possibly answer that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.