Writing into different files.

Hello all,

I just need a small help,i am not familiar with writing into multiple files.Here is my program,in this based upon the id it has to write to multiple files all by iteself.

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
  // Positions.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include<sstream>
#include<fstream>
#include<iostream>
#include <cstdlib>
#include <algorithm>
#include<list>
#include<math.h>
#include<conio.h>
#include<string>
using namespace std;

ifstream fileinput;
ofstream fileoutput;

void fileout(int ID, int tag, double& temp6)
{
    stringstream ss;
    ss <<"Id_"<< ID << ".txt";
    std::string filename = ss.str();
    // cout << "ID: " << ID << "\t Filename: " << filename << endl;
    ofstream myout(filename.c_str(), ios::app);
    myout << tag << temp6<<"\t";
    myout.close();
    ss.clear();
}
int main(){
int count=0;
   long line_No=0;
   fileinput.open("12.txt");
   fileoutput.open("output.txt");
   if(!fileinput) { // file couldn't be opened.
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }
   while(!fileinput.eof()){
	  std::string line,id,timestep,word,wordOne,temp6,temp7,temp8,temp,tag;
			getline(fileinput,line);
			stringstream linestring(line);
			linestring >> id  >> tag>> word >> temp8 >>temp6>>temp7;
			

			if (word == "Position")
			{
				double d;
				stringstream s1(temp6);
				s1 >> d;

		//cout<<tag<<" "<<temp8<<endl;
			
				//istringstream istring (id);
                //int temp_id;
                //istring >> temp_id;
				//fileout(temp_id, temp.tag, temp.temp7);
				
				


				
			}
       }

}
This is a bit over my head. But if I were trying to write to different files, I would just use a seperate file stream for each file.
Yeah what Manga said, simply open up the file you want to modify, stream in the modification, close the file and move along to the next one...
Topic archived. No new replies allowed.