ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I am new to Linux and C++.
I want to compare two files and print the values in first file which are not present in second file to the third file. E.g
File1 File2 File3
1 1 2
2 3 4
3 5
4 7
5 9
11
As we can see, values from File1 i.e 2 and 4 are not in file2 and have been printed to file3. I know we have to use arrays. Please help me.
Hi,
1) Is that a homework?
2) If not, is there a reason why you want to do this in C++?
3) Can you show us what you have so far?
4) Are those files sorted?
Last edited by millgates; 02-13-2012 at 05:30 AM.
Reason: typo
No, this is not a homework. This is an officework. I can do the sorting myself. But, I am out of touch with programming and basically want to read from file and put into an array. But if someone can help me with the complete work, I'd be very grateful. It can be in java also not necessarily in C++.
But I'm completely out of touch with java. I can only remember how to compile now!
If you insist on using C++, loading a list of ints into memory could look like this.
Code:
#include <iostream>
#include <fstream>
#include <list>
int main(){
std::ifstream file1("file1", std::ios::in);
if (!file1) { return 1; }
std::list < int > list1;
int temp;
while ((file1 >> temp)) {
list1.push_back(temp);
std::cout << temp << std::endl;
}
// do someting with list
file1.close();
return 0;
}
You may want to take care of some details such as handling files that contain other characters than digits as that would cause an infinite loop here, but this could be a start.
[edit]
:-[ I've been beaten and overclassed by the previous post.
Diff is the tool you want.
<code>man diff</code>
I thought you could make it show only the lines that were missing from one file, but maybe you need to 'pipe' it through grep. (grep Can get pretty complicated, but it's useful to know you don't need to construct a 'regular expression'. Just giving the string you are looking for as an argument to grep qualifies... in most cases.)
@millgates :
No 11 was in file2. Improper formatting I guess in the code
I tried diff and the code you put, but it seems to put show all the unique entries from both the files.
@Weapon S : Thank you for dedicating your time as well.
Thank you to both of you. But I created my own code somehow in haste and I haven't checked the redundancy of LOCs. But it worked for now.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#define NOT_FOUND -1
#define MAX 1100
using namespace std;
int search( const int arr[], int target, int n );
void showAry( int arr[], int n );
int main()
{
int x[MAX],y[MAX];
int index,sizex=0,sizey=0;
int c, target, m,i,j,n;
FILE* fin,fin2;
fin=fopen("data.txt", "r");
if(fin==NULL)
{
printf("Error opening file ... Press 'Enter' to exit ... ");
getchar();
return -1;
}
/* m holds the number elements when loop has reached EOF */
for (m = 0; fscanf(fin, "%d" , &x[m]) != EOF ; ++m) {;}
fclose (fin);
fin=fopen("data2.txt", "r");
if(fin==NULL)
{
printf("Error opening file ... Press 'Enter' to exit ... ");
getchar();
return -1;
}
for (m = 0; fscanf(fin, "%d" , &y[m]) != EOF ; ++m) {;}
fclose (fin);
//for (m=0;m<5;m++)
//cout<<x[m]<<endl;
//sizex=0;
while(x[sizex]) sizex++;
//cout<<"\n"<<(size-1)<<endl;
//for (m=0;m<5;m++)
//cout<<y[m]<<endl;
while(y[sizey]) sizey++;
cout<<endl;
for(i=0;i<sizex;i++)
{ n=0;
for(j=0;j<sizey;j++)
{
if(x[i]==y[j])
break;
else
{
n++;
if(n==sizey) cout<<x[i]<<endl;
}
}
}
//getchar();
cout<<endl;
return(0);
}
void showAry( int arr[], int n )
{
int i;
for( i=0; i<n; ++i )
printf( "%d ", arr[i] );
}
int search( const int arr[], int target, int n )
{
int i;
for( i=0; i<n; ++i )
if( target==arr[i] ) return i;
return -1;
}
Usage : The program when executed compares the content of data.txt with data2.txt and outputs the content of data.txt not available in data2.txt to the screen.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.