using data from multiple files

I need to use data from three txt files. In file 1 there are 13 columns, in file 2 there are 3 columns and in file 3 there is 1 column.
I want to prepare a 4th file with
first 3 columns of file 1
1 column of file 2
1 column of file 3

I am opening all txt files (file1, file 2 and file 3) together. Reading the data using the while loop.
It is showing 'Build error' after run!

please help!
show your code please
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<fstream>

using namespace std;

void main()
ifstream temp("temp.in");
	ifstream temp1("temp1.in");
	ifstream soil("soils.in");

	float col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15;
	
		while(soil>>col1>>col2>>col3>>col4>>col5>>col6>>col7>>col8>>col9>>col10>>col11>>col12>>col13)
			while(temp>>col14)



I think i m doing mistakeby using two 'while' loop!! but do not understand how to solve it!!
Last edited on
Upto to my knowledge, if you put something inside the while brackets() , its a condition. So it just check and doesnt store values in the variables.

Sorry for the misguidance
Last edited on
There is noting wrong with putting the input inside the while parentheses, that is actually one of the preferred ways of reading input in a C++ program.

Really this program is screaming for the use of an array, or vector, possibly a structure.

Your problem is that you only have one of each of the variables, so if your file contains more than one you will have problems. I really recommend that you read each file in it's own loop, don't try to read multiple files in the same loop.


Jib

please expand!!! how can I read multiple file in same loop??
I said I don't recommend using the same loop. Use different loops to read each file.

Sorry!! I had mistyped!! If I am reading the files separately using the wile loop, then how I will merge the data(columns) from separate files into one??
If you are going to try to combine these three files into a third file, this is why I recommend using vectors to hold the data from the three files.

Please show a small sample of your three input files. And also a small sample of what the output file should look like.
EXAMPLE OF THE FILE:

file 1

1 2 1.2 2.5 3.6 3.66 4.55 8.1
5 10 1.2 3.2 5.6 3.33 4.25 8.1
25 rows

file 2

1.3
1.3
1.3

25 rows

file 3

7.8
7.8
7.8
25 rows

the output file (file4)

1 2 1.3 2.5 3.6 3.66 4.55 7.8
5 10 1.3 3.2 5.6 3.33 4.25 7.8
25 rows


In this case you can read all three files in a single while loop. I would recommend that you first read the information from the second or third file first, since they only contain one item per line. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
float from3, from2;
float from1[SIZE]; // Where SIZE is a constant that corresponds to the number of items per line in file1. 
// open your files, make sure you insure the opening succeeded. 

while(file3 >> from3)
{
   file2 >> from2;
   if(!file2)
   {
       // Error reading from file 2 do something.
   }
   for(int i = 0; i < SIZE; ++i)
   {
      file1 >> from1[i];
   }
   if(!file1)
   {
       // Error reading from file 2 do something.
   }
   // Now write to your fourth file using the above information.
}


It is important to always check that your read and write operations succeed and if not either stop processing and tell the user about the problem or something else. But always check.
Last edited on
Topic archived. No new replies allowed.