The program i am working on currently need to read a file and write into multiple files. Basically i am supposed to separate all the classes in tat file so i actually added an comment line to the file, i extract the classes according to the comments. Now i would like to write all the classes into different files,example test1.txt,tes2.txt,test3.txt.......this is my coding

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>


using namespace std;



int main ()
{

    char str[99999];
    char counter = '0';
    string str2, str3, str4;
    size_t pos, pos1;
  
 
    fstream file_op("c:\\Fuse_cortex1.h",ios::in);
    file_op.getline(str,99999,'@');
  
    string s = str;

	for(int i =0; i<40; i++)
	{
		if (counter == '0')
		{
			pos = s.find("//header#");    
			str2 = s.substr (pos);
			pos1=str2.find("//header*");
			str3 = str2.substr(0,pos1+9);
			counter++;
			
			fstream file_op1("c:\\header.txt",ios::out);
			file_op1<<str3;
			file_op1.close();
			i++;
		}

		else 
		{
			string start_temp = "//cls";
			string end_temp = "//cls";
			start_temp = start_temp + counter + "#";
			end_temp = end_temp + counter + "*";
			pos = s.find(start_temp);    
			str2 = s.substr (pos);
			pos1=str2.find(end_temp);
			str4 = str2.substr(0,pos1+8);
			counter++;
/*tis is the part i need help*/      fstream file_op2("c:\\arun.txt",ios::out);
                          file_op2<<str4;
                          file_op2.close();
			i++;
  		}
	}
    
    


  system("pause");
  return 0;
}

Not sure what the actual problem is, but maybe you are missing ios::ate so that the file does not get truncated each time you open it, i.e.

file_op2("c:\\arun.txt", ios::out|[B]ios::ate[/B]);

i wan to write into different files......i am using a loop so i wan the name to be generated automatically...and not as in the coding.....if its like in the coding then the classes will be written in 1 file....i wan to be like, when the first class is found i want it to write into a new file maybe test.1 so it continues till the last class is detected..but i wan it in the loop format....i wan the path of the file to be generated automatically with different names so each class will be written inside different files..

i wan to write into different files......i am using a loop so i wan the name to be generated automatically...and not as in the coding.....if its like in the coding then the classes will be written in 1 file....i wan to be like, when the first class is found i want it to write into a new file maybe test.1 so it continues till the last class is detected..but i wan it in the loop format....i wan the path of the file to be generated automatically with different names so each class will be written inside different files..

Then put the filename together like

[B]int[/B] counter = 1;
stringstream sstrm;
sstrm << "c:\\file#" << counter << ".txt";
string filename = sstrm.str();
cout << filename;

u could actually cout the filename...but i need to write into tat filename, when i use the fstream function and include
fstream file_op2(filename,ios::out);..... it returns error

Error 1 error C2664: 'std::basic_fstream<_Elem,_Traits>::basic_fstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const char *' c:\documents and settings\agnanesv\my documents\visual studio 2005\projects\testing\testing\testing.cpp 61

string start_temp = "//cls"; string end_temp = "//cls"; start_temp = start_temp + counter + "#"; end_temp = end_temp + counter + "*";

is more less the same with the coding u used, i tried using this method too.....i can cout the filename but cant get it to write into tat filename....

It has to be ...

fstream file_op2(filename.[B]c_str()[/B], ios::out);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.