importing data from column

I have got two files:

input_file.txt
data 1 data 2 data 3
4.5 1.25 0.03
4.6 1 0.01
7.5 2.25 0.5
7.8 1.35 0.6
10.2 1.25 0.4
8.2 1.525 0.02
8.4 1.56 0.01
6.5 1.595 0.1
9.5 1.63 0.6
3.3 1.665 0.4
4.5 1.7 0.631
8.5 1.735 0.766
9.4 1.77 0.901
4.5 1.805 1.036

main_file.txt
2.00000 'IVAUTO'
2.00000 'NELEM'
53.2 'SITLAT'
-1.3 'SITLNG'
0.21000 'SAND'
0.28000 'SILT'
0.50000 'CLAY'
0.000 'ROCK'
1.20000 'BULKD'
9.00000 'NLAYER'
4.00000 'NLAYPG'
1.00000 'DRAIN'
0.90 'BASEF'
0.00000 'STORMF'
8.000 'PRECRO'
0.15 'FRACRO'
0.00000 'SWFLAG'
8.20000 'PH'
1.00000 'PSLSRB'
10.00000 'SORPMX'

I need to take the input of each row from the 'input_file.txt' column 'data1' and change the value of 'PH' in the main_file.txt and input of each row from the 'input_file.txt' column 'data2' and change the value of 'BULKD' in the main_file.txt. After changing values in the main_file.txt with the input from each row of the input_file.txt one file will be created like, main_file_1.txt (for row 1); main_file_2.txt (for row 2) and so on......

i could not understand how to start the algorithm for this program. any help will be highly appreciated!!

So, in other (pseudo) words:
for each line in input_file.txt
create main_file_X.txt from main_file.txt
where
  if col2==PH, col1 := data1
  if col2==BULKD, col1 := data2


Does it matter whether the rows are reordered in the output files?
I did the program like this
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
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
	float data1, data2, data3, data4;
	char col1[100];

	
	for(int i=1; i<31; i++)
	{
		stringstream aa;
		aa<<"out_"<<i<<".txt";
		string out = aa.str();

	
	ifstream input("input_file.txt");
	ifstream site("main_file.txt");
	ofstream file(out);

	while(site>>data4>>col1 || input>>data1>>data2>>data3)
	{
			if(col1=="'PH'")
				data4 = data1;			
		file<<data4<<'\t'<<col1<<endl;
				
	}
	}

	system("pause");
}


but it is not replacing the PH with the corresponding value in the input_file.txt
A pointer == another pointer only if they hold the same address. One pointing to string literal means automatics failure. std::string is your friend.

You do have other logical issues too.

If you would just want the job done, rather than to use C++ or to learn programming, you could:
let NUM=0
while read data1 data2 dum
do if [[ "${data1}" != "data" ]]
       then let NUM=${NUM}+1
       sed "s/^.* 'PH'/${data1} 'PH'/; s/^.* 'BULKD'/${data2} 'BULKD'/" main.txt > out_${NUM}.txt
     fi
done < input.txt


With C++ one could do:
1. Read main.txt into a std::vector< std::pair< std::string, std::string > >
2. Create iterators to the two elements (PH and BULKD) of the vector
3. Open input.txt
4. While input.getline
4a. separate the two std::string data values from line
4b. update the two elements
4c. write vector to out_i.txt
This solution assumes that the text "data 1 data 2 data 3" is not the first line of the input file.



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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include <iomanip> 

int main()
{
  const char* inputfilename = "input_file.txt";
  const char* mainfilename = "main_file.txt";
  	
  std::ifstream input(inputfilename);
  if(!input){
  	std::cout<<"Unable to open "<<inputfilename<<std::endl;
  	return 1;
  }
  
  std::ifstream site(mainfilename);
  if(!site){
  	std::cout<<"Unable to open "<<mainfilename<<std::endl;
  	return 2;
  }
  
  int numlines = 20; // number of lines in main_file.txt
  std::string mainlines[numlines]; // array to hold lines of main_file.txt
  int index = 0;
  
  while((index < numlines) and (getline(site,mainlines[index]))){ //loop thru lines of main_file.txt
  	mainlines[index++] +='\n';
  }
  
  float data1, data2, data3;
  int count = 1;
  std::string ph = "'PH'\n";
  std::string bulkd = "'BULKD'\n";
  
  while(input>>data1>>data2>>data3){ // loop thru lines of input_file.txt
  	 std::stringstream outfilename, line1, line2;
  	 outfilename<<"main_file_"<<count++<<".txt";
  	 line1<<std::fixed<<std::setprecision(5)<<data1<<" "<<ph;
  	 line2<<std::fixed<<std::setprecision(5)<<data2<<" "<<bulkd;
  	 
  	 std::ofstream outfile(outfilename.str().c_str());
  	 
  	 if(outfile){
  	   for(int i = 0; i < numlines; ++i){
  	   	 switch(i){
  	   	 	case 8: outfile<<line2.str();break;
  	   	 	case 17: outfile<<line1.str();break;
  	   	 	default: outfile<<mainlines[i];
  	   	 }
  	   }
  	   outfile<<std::endl;
	   std::cout<<outfilename.str()<<" written"<<std::endl;	
  	 }
  	 else{
	   std::cout<<"Unable to create "<<outfilename.str()<<std::endl;
  	 }
  }
  std::cout<<"Finished"<<std::endl;
  return 0;
}
Thank you very much vin, that is really helpful....
Topic archived. No new replies allowed.