Multiple Files

Hello All,

I am new to c++ programming i just want to know how to write the data into different files.Suppose my input files has 1-8 ids so each id data must be stored into each different file. And below is my code.
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<fstream>
#include<iostream>
#include <cstdlib>
#include <algorithm>
#include<list>
#include <math.h>
#include<conio.h>
#include<string>
#define PI 3.14159265
using namespace std;
double angle;
ifstream indata;
ofstream outfile;
int main(){
   int count=0;
   string str;
   string str2;
   indata.open("out1");
   outfile.open("output.txt");
   if(!indata) { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }
   while(!indata.eof()){
	   count=count+1;
       getline(indata,str);
	   unsigned found=str.find("Rotation matrix");
	   int cycle_number=1;
	   double tag=0.0;
	   int loop_number=0;
	   double sum_of_diagonal=0.00;
	   if(found!=string::npos){
		   while( cycle_number<=3){
               int i=0;
			   while (str[i]){
				  char c=str[i];
				  if(c==' '){
					  loop_number=loop_number+1;
					  if( cycle_number==1&& loop_number==5){
						  sum_of_diagonal=sum_of_diagonal+atof(str2.c_str());
					  }else{
						  if( cycle_number==2 && loop_number==4){
							  sum_of_diagonal=sum_of_diagonal+atof(str2.c_str());
						  }

					  }
					 // cout<<"str:"<<str2<<endl;
					  str2="";
					  i++;
				  }else{
					 str2+=c;
					 i++;
				  }

			   }
           if( cycle_number==3){
			   sum_of_diagonal=sum_of_diagonal+atof(str2.c_str());
			  /* if( sum_of_diagonal<0){
				   cout<<"stop"<<endl;
			   }*/
			   //outfile<<sum_of_diagonal<<'\n';
			angle = acos ((sum_of_diagonal-1.0)/2.0) *180/PI;
			 outfile<<angle<<'\n';
		   }
		   if( cycle_number<=3){
			  getline(indata,str);
			  loop_number=0;
		   }
		   str2="";
		   cycle_number=cycle_number+1;
		   }
	   }

   }

   return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Open file 1
std::ofstream fout( "file1.txt" );

// write things to it

// Close file 1
fout.close();

// Open another file
fout.open( "file2.txt" );

// write to it

// Close it again
fout.close();

// And so on :) 

Last edited on
Topic archived. No new replies allowed.