basically i have 4 files 2 contain the following data . Trying to find out data which is common and not common in both files and write it out to files common and not common. It almost works except for it doesn't compare the last item in the list before it ends. :(
Thanks.

Final1.txt
3
6
9
12
15
18
21

Final2.txt
9
12
15
18
21
24
27
30
33
36

--stores numbers which are common
Common
9
12
15
18
doesn't get this number: 21

Not common
3
6
24
27
30
33
36

#include <iostream>
#include <fstream>

using namespace std;
//open file to read  from
void intializeList(ifstream& List, char* input);

//open file to write to
void intializeoutput(ofstream& List, char* output);

//getitem from file
int getNextitem(ifstream& file);

//output item to correct file
void setnextitem(ofstream& file, int number);


int main()
{
	int moreItems;


	ifstream Final1,Final2;
	ofstream Common,Notcommon;

	//open input and output files
	intializeList(Final1,"Final1.txt");
	intializeList(Final2,"Final2.txt");
	intializeoutput(Common,"Common.txt");
	intializeoutput(Notcommon,"Notincommon.txt");
	
	//get first item in both lists
	int list1=getNextitem(Final1);
	int list2=getNextitem(Final2);
	moreItems=list1&&list2;
	
	//loop until no items in one list
	while(moreItems)
	{
		//item from list 1 smaller than item from list 2
		if(list1<list2)
		{
			cout<<"list1<list2\n";
			//write out to not common file
			setnextitem(Notcommon,list1);
			//get next value in list1
			list1=getNextitem(Final1);
		}
		//items are equal 
		else if(list1=list2)
		{
			cout<<"list1=list2\n";
			setnextitem(Common,list1);
			list1=getNextitem(Final1);
			list2=getNextitem(Final2);
		}
		else
		{
			cout<<"list1>list2\n";
			//write out to not common file
			setnextitem(Notcommon,list2);
			//get next value in list2
			list2=getNextitem(Final2);
		}
		if(Final1.eof()||Final2.eof())
		{
			break;
		}
	
	}
	//read in last item from the list which and then 

	//copy all the remaining data from the list which isn't empty to not in common
	while(!Final1.eof())
	{
		list1=getNextitem(Final1);
		setnextitem(Notcommon,list1);
	}

	while(!Final2.eof())
	{
		list2=getNextitem(Final2);
		setnextitem(Notcommon,list2);
	}
	


	Final1.close();
	Final2.close();
	Common.close();
	Notcommon.close();
	

	return 0;
}

void intializeList(ifstream& List, char* input)
{
	List.open(input);
	if(List.fail())
	{
		cout<<"error opening file"<<input<<"\n";
	}
	else
	{
		cout<<"file input named "<<input<<" opened\n";
	}

}

void intializeoutput(ofstream& List, char* output)
{
	List.open(output);
	if(List.fail())
	{
		cout<<"error opening file"<<output<<"\n";
	}
	else
	{
		cout<<"file output named "<<output<<" opened\n";
	}
}

int getNextitem(ifstream& file)
{
	int data;
	//reached end of file
	if(file.eof())
	{
		cout<<"end of file reached\n";
		file.close();
	}
	else
	{
		file>>data;
		return data;
	}
	
}

void setnextitem(ofstream& file, int number)
{
	cout<<number<<endl;
	file<<number<<"\n";
}

Edited 6 Years Ago by lotrsimp12345: n/a

one of the best ways to execute two loops at the same time is threads, im gonna show you one way to do it but there are many more :)

#include <windows.h>
#include <iostream>

using namespace std;

HANDLE hThread;
DWORD  dwThreadId;

void Loop1();
void Loop2();

int main() {
    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Loop2, NULL/*function params here*/, NULL, &dwThreadId);
    Loop1();

    return(0);
}

void Loop1() {
    while(true) {
        cout << "Loop 1 running \n" << endl;
    }
}

void Loop2() {
    while(true) {
        cout << "Loop 2 running \n" << endl;
    }
}

basically i have 4 files 2 contain the following data . Trying to find out data which is common and not common in both files and write it out to files common and not common. It almost works except for it doesn't compare the last item in the list before it ends. :(
Thanks.

Final1.txt
3
6
9
12
15
18
21

Final2.txt
9
12
15
18
21
24
27
30
33
36

--stores numbers which are common
Common
9
12
15
18
doesn't get this number: 21

Not common
3
6
24
27
30
33
36

#include <iostream>
#include <fstream>

using namespace std;
//open file to read  from
void intializeList(ifstream& List, char* input);

//open file to write to
void intializeoutput(ofstream& List, char* output);

//getitem from file
int getNextitem(ifstream& file);

//output item to correct file
void setnextitem(ofstream& file, int number);


int main()
{
	int moreItems;


	ifstream Final1,Final2;
	ofstream Common,Notcommon;

	//open input and output files
	intializeList(Final1,"Final1.txt");
	intializeList(Final2,"Final2.txt");
	intializeoutput(Common,"Common.txt");
	intializeoutput(Notcommon,"Notincommon.txt");
	
	//get first item in both lists
	int list1=getNextitem(Final1);
	int list2=getNextitem(Final2);
	moreItems=list1&&list2;
	
	//loop until no items in one list
	while(moreItems)
	{
		//item from list 1 smaller than item from list 2
		if(list1<list2)
		{
			cout<<"list1<list2\n";
			//write out to not common file
			setnextitem(Notcommon,list1);
			//get next value in list1
			list1=getNextitem(Final1);
		}
		//items are equal 
		else if(list1=list2)
		{
			cout<<"list1=list2\n";
			setnextitem(Common,list1);
			list1=getNextitem(Final1);
			list2=getNextitem(Final2);
		}
		else
		{
			cout<<"list1>list2\n";
			//write out to not common file
			setnextitem(Notcommon,list2);
			//get next value in list2
			list2=getNextitem(Final2);
		}
		if(Final1.eof()||Final2.eof())
		{
			break;
		}
	
	}
	//read in last item from the list which and then 

	//copy all the remaining data from the list which isn't empty to not in common
	while(!Final1.eof())
	{
		list1=getNextitem(Final1);
		setnextitem(Notcommon,list1);
	}

	while(!Final2.eof())
	{
		list2=getNextitem(Final2);
		setnextitem(Notcommon,list2);
	}
	


	Final1.close();
	Final2.close();
	Common.close();
	Notcommon.close();
	

	return 0;
}

void intializeList(ifstream& List, char* input)
{
	List.open(input);
	if(List.fail())
	{
		cout<<"error opening file"<<input<<"\n";
	}
	else
	{
		cout<<"file input named "<<input<<" opened\n";
	}

}

void intializeoutput(ofstream& List, char* output)
{
	List.open(output);
	if(List.fail())
	{
		cout<<"error opening file"<<output<<"\n";
	}
	else
	{
		cout<<"file output named "<<output<<" opened\n";
	}
}

int getNextitem(ifstream& file)
{
	int data;
	//reached end of file
	if(file.eof())
	{
		cout<<"end of file reached\n";
		file.close();
	}
	else
	{
		file>>data;
		return data;
	}
	
}

void setnextitem(ofstream& file, int number)
{
	cout<<number<<endl;
	file<<number<<"\n";
}

Quick and dirty way to do this:

1) Read both files contents into an array for each file Ex: f1[] and f2[] (this can be an array of strings or a 2 dimensional array - it will of course have to either know the max length of each file or dynamically grow each file content array as you read)

2) have an array of booleans corresponding to each file content array Ex: f1_check[] - f1_check[x] will store true if the f1[x] is also in f2[x], f1_check[x] will store false if the content at f1[x] is ONLY in f1[x].


3) Do a nested for loop going through both f1 and f2 (put f1 on the outer loop), searching for each element of f1 in f2. If f1 IS found in f2[j] (j being the inner for loop index variable) - set f1_check to true. If f1 is NOT found in f2[j] (j being the inner for loop index variable) - set f1_check to false.

4) Do a nested for loop going through both f1 and f2 (now put f2 on the outer loop), searching for each element of f2 in f1. If f2 IS found in f1[j] (j being the inner for loop index variable) - set f2_check to true. If f2 is NOT found in f1[j] (j being the inner for loop index variable) - set f2_check to false.

5) At this point you have two arrays, f1_check and f2_check that have booleans at each index indicating which elements of f1 or f2 are common (the ones with "true" values) and which are only in one array (the ones with "false" values).

6) Loop through f1_check and if f1_check is true, print f1 to the common file. if f1_check is false, print f1 to the "f1 only" file (file that represents values that are only in the first file and not the second.

6) Loop through f2_check and if f2_check is true, (do nothing because the common values were already printed from f1_check). if f2_check is false, print f2 to the "f2 only" file (file that represents values that are only in the second file and not the first.

This question has already been answered. Start a new discussion instead.