combining 2 different file

hello guys ...

I want to know your opinion about how to merge 2 different files ... for instance , File 1 contains

1 2 3 4
3 2 2 4
2 1 1 1
2 1 1 1
3 4 5 6
3 4 5 6
3 5 6 7

and File 2 contains

3 3.098
3 6.097
3 8.900
3 7.8

From these 2 files, you can see the number that is bold. You can see that, in File 1 , the bold number is 1 2 3 , whereas in File 2 the bold number is 3 . What I want to do is :

If the bold number in File 1 is 3 , then it will return the value of File 2 . For example :

In File 1, the second row shows 3 (bold number) , so it will return 3.098
and if the bold number is not 3, then it will return 100 ..

so the output should be like this :

1 2 3 4
3 2 2 4
2 1 1 1
2 1 1 1
3 4 5 6
3 4 5 6
3 5 6 7

100
3.098
100
100
6.097
8.900
7.8

I have tried code the program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(int i=0;i<noofneighbormesh;i++)
	{
		infile1 >> dummy1 >> n[i][0] >> n[i][1] >> n[i][2];
	
		if(dummy1 == 3)
		{
				infile2 >> dummy2 >> eva_mesh[i];
				outfile << eva_mesh[i] << endl;
			
		}

		else 
		{
			outfile << "100" << endl;
		}
	}


can anyone help me on this problem ? thank you so much!!
closed account (4Gb4jE8b)
looks a lot like a homework problem to me.... *points to http://www.cplusplus.com/articles/how_to_ask/ where it specifically says don't ask homework problems*

but sadly i don't really know how to help you out, as i too am a newbie. I would suggest reading up on fstream and stringstream, they might be able to help you
I'm new here, but I think I have your code working. Two main comments:

First of all, there is no apparent need to keep a record of all the n variables, so a 2D array is not necessary. You can simply have an array of three int's, n[3], and overwrite them each time given you seem to be trashing them. You could even make it an array of 4 int's, n[4] and test n[0] as your dummy1 to clean up the code.

The other comment is more to the point. You need a different index for the eva_mesh array (and specifically for infile2) than the index being used to iterate through infile1, because the two files are not necessarily the same length, including in your example. While you may iterate through 4 lines from infile1 that don't start with a "3", you only want to have iterated once to the next line of infile2 when you reach the next "3". That bit of logic should fix your code. See bellow for what I've done. Not the best code, but I tried to change as little as possible so you could match it to yours.

As for it potentially being homework. If it offends people I answered this in such detail, I'm sorry but I'm new and going through the exercise helps me learn myself. Also, it only hurts sitikhadijahali if he doesn't learn this on his own for a class. Also, seems to me it's for a FEM application and not a CS class. Anyways, cheers to all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char **argv)
{
	int noofneighbormesh = 7;
	int i, j=0, dummy2;
	int n[4];
	double eva_mesh[4];

	if(argc != 4)
	{
		cout << "Usage: ./exec infile1 infile2 outfile\n";
	}

	fstream infile1 (argv[1]);
	fstream infile2 (argv[2]);
	fstream outfile (argv[3]);

	for(i=0; i<noofneighbormesh; i++)
	{
		infile1 >> n[0] >> n[1] >> n[2] >> n[3];

		if(dummy1 == n[0])
		{
			infile2 >> dummy2 >> eva_mesh[j];
			outfile << eva_mesh[j] << endl;
			j++;
		}
		else 
		{
			outfile << "100" << endl;
		}
	}
}

Topic archived. No new replies allowed.