hi guys, i'm having a bit of a prolem with C++, i've been programming c# for quite a while now, so i am not a novice lol.
problem -- i've created the following ofstream in my main method:

ofstream out1("array1.txt");

however later on in the code i call

out1 << arrayStorage1;

which i have overloaded the operators as.....(in my ArrayIntStorage class)

ostream& operator<< (ostream &out, ArrayIntStorage &ais)
{
	ais.write(); 
	return out;
}

and my write method looks like this:

void ArrayIntStorage::write(char fileName[])
{
	ofstream out(fileName);

	for(int i = 0; i< arrayLength; i++)
	{
		out << arrayNoSort[i] << endl;
	}

	out.close();
}

my problem is that i don't want to hardcode the filename ("array1.txt") as i'll need to use this again for create other files, eg; array2.txt and so on, i was wondering if there is a way to get the name of ofstream(*filename*) (in the main method) and use that to specify the filename when i come to write it.

thanks in advance

ryan

Recommended Answers

All 8 Replies

#include <iostream>
#include <fstream>

using namespace std;

template <typename C> struct write_file
  {
    C data;
    char* file;

    write_file() {}
    ~write_file() {}

    friend ostream& operator<< (ostream& o,write_file f) {return o<<f;} 

    write_file(char* f,C d) : data(d),file(f)
      {
        ofstream out(file,ios::app);
        out << data << endl;
        out.close(); 
      }              
  };
  
int main ()
  {
    char* filename="file.txt";
    string something_to_write="asd";
    int something_else=2010;

    write_file<string>(filename,something_to_write);
    write_file<int>(filename,something_else);
  }

Why don't you just pass ostream &out into your write function and write to the provided stream?

Why don't you just pass ostream &out into your write function and write to the provided stream?

how?

here is a more detailed example of my code......

int main(int argc, char **argv) {

	// ***********************************
	// non-sort read & then sort using std
	// ***********************************
	ifstream fin1("ACW2_data.txt");
	ofstream out1("array1.txt");
	ofstream out2("array2.txt");

	if(!fin1.is_open()) 
	{
		cout << "FAIL:" << endl;
		return 1;
	}

	ArrayIntStorage arrayStorage1;
	arrayStorage1.setReadSort(false);	// do not read sort

	// read in int values into data structure
	fin1 >> arrayStorage1;

	// output int values in data structure to file
	out1 << arrayStorage1;

	// sort data structure using std
	arrayStorage1.sortStd();

	// output int values in data structure to file
	out2 << arrayStorage1;

	fin1.close();
	out1.close();
	out2.close();
using namespace std;

ArrayIntStorage::ArrayIntStorage(void)
{
}

ifstream& operator>> (ifstream &in, ArrayIntStorage &ais) 
{
	ais.read("ACW2_data.txt");
	return in;
}

ostream& operator<< (ostream &out, ArrayIntStorage &ais)
{
	//ais.write(out);
	return out;
}

void ArrayIntStorage::read(char fileName[])
{
	ifstream fin(fileName); 

	if(!fin.is_open())
	{
		cout << "Can't find file!" << endl;
		exit(0);
	}
	fin.ignore(19);

	for(int i = 0; i < arrayLength; i++)
	{
		fin >> arrayNoSort[i];
		cout << arrayNoSort[i] << endl;
	}

	fin.close();
}

void ArrayIntStorage::write(char fileName[])
{
	ofstream out(fileName);

	for(int i = 0; i< arrayLength; i++)
	{
		out << arrayNoSort[i] << endl;
	}

	out.close();
}

i don't know how to pass a filename to my write method, from my ofstream operator<< (overload method) :(

thanks

Even if you could, which you can't because as someone as pointed out else where ostream might not be a file stream it could be any stream including one without a file name, but even if you could the open in your write function would fail on some operating systems because the file is already open.

I didn't say pass the file name I said pass the output stream.

Your write function would be declared

void ArrayIntStorage::write(ostream& out)
{
    // Code to write to out
}

and you would call it as per line 15

keep getting error C2664

ostream& operator<< (ostream &out, ArrayIntStorage &ais)
{
	ais.write(out);
	return out;
}

void ArrayIntStorage::read(char fileName[])
{
	if (_sortRead == false)
	{
		ifstream fin(fileName); 

		if(!fin.is_open())
		{
			cout << "Can't find file!" << endl;
			exit(0);
		}
		fin.ignore(19);

		for(int i = 0; i < arrayLength; i++)
		{
			fin >> arrayNoSort[i];
			cout << arrayNoSort[i] << endl;
		}

		fin.close();
	}
}

void ArrayIntStorage::write(ostream& out)
{
	ofstream outStream(out); //error C2664!

	for(int i = 0; i< arrayLength; i++)
	{
		outStream << arrayNoSort[i] << endl;
	}

	outStream.close();
}

Did you change the declaration of the function ArrayIntStorage::write as well as the definition?

If you are going to post warning messages post the entire message because the text often contains extra details, the line number can be useful and to anyone not familiar with the Microsoft compiler the number is completely meaningless.

Opps missed your comment another reason to post the entire message.

What are you doing trying to open and close an already open stream? out is a working output stream just use it.

lol, i missed that one aswell,

all is working sweetly now!

thanks pal!

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.