The problem is that i need to save in simple way the array of structure:

struct gydytojas {
   int gydid, amzius, specialyb, telefonas, asmkod;
   String vardas[25];
   String pavarde[35];
   String adresas[50]; };

    gydytojas gydmas[100];

This is the struct & array.

Code:

gydytojas pgyd;
pgyd.vardas[25]=Edit21->Text;
pgyd.pavarde[35]=Edit22->Text;
pgyd.adresas[50]=Edit24->Text;
pgyd.asmkod=StrToInt(Edit23->Text);
pgyd.amzius=StrToInt(Edit27->Text);
pgyd.gydid=StrToInt(Edit26->Text);
pgyd.telefonas=StrToInt(Edit25->Text);
pgyd.specialyb=ComboBox5->ItemIndex;
gydmas[1]=pgyd;
ofstream fr("Gydytojai.inf");
for( int i = 1; i<2; i++) { fr << gydmas[i];}
fr.close();

(This is the creation of that file also, later i will ned to read and add the right number of array..)
I want to save an array to one file without using SaveDialog / OpenDialog( i can't choose file from which i could read/save so no printf, scanf could be used) , and would like to read from that file data.

As you see i tried to use ofstream, but there was an error :
[C++ Error] Unit1.cpp(180): E2094 'operator<<' not implemented in type 'ofstream' for arguments of type 'TForm1::gydytojas'

Recommended Answers

All 9 Replies

You can still use ofstream, but you have to overload the << operator for your struct:

struct gydytojas {
  int gydid, amzius, specialyb, telefonas, asmkod;
  String vardas[25];
  String pavarde[35];
  String adresas[50];

  friend ostream& operator<<(ostream& os, const gydytojas& obj);
};

ostream& operator<<(ostream& os, const gydytojas& obj)
{
  // Print the members to os like you would to cout

  return os;
}

You can still use ofstream, but you have to overload the << operator for your struct:

struct gydytojas {
  int gydid, amzius, specialyb, telefonas, asmkod;
  String vardas[25];
  String pavarde[35];
  String adresas[50];

  friend ostream& operator<<(ostream& os, const gydytojas& obj);
};

ostream& operator<<(ostream& os, const gydytojas& obj)
{
  // Print the members to os like you would to cout

  return os;
}

Thanks for explanation, but after i add

ostream& operator <<(ostream& os, const gydytojas& obj) 
{

return os;
}

Get this error.
[C++ Error] Unit1.h(234): E2080 'TForm1::operator <<(ostream &,const TForm1::gydytojas &)' must be declared with one parameter

And if i need to read that from that file i should create another friend istream& operator, right?

It looks like what you did is add the overloaded operator as a member function instead of a friend, and that's different from Ed's code.

It looks like what you did is add the overloaded operator as a member function instead of a friend, and that's different from Ed's code.

Abit confused, so where i should put this thing ?

ostream& operator<<(ostream& os, const gydytojas& obj)
{
  // Print the members to os like you would to cout

  return os;
}

I thought, it should be in private declaration of Unit1.h.

Edward showed you exactly where to put it. But you can make it even simpler like this:

struct gydytojas {
  int gydid, amzius, specialyb, telefonas, asmkod;
  String vardas[25];
  String pavarde[35];
  String adresas[50];

  friend ostream& operator<<(ostream& os, const gydytojas& obj)
  {
    // Print the members to os like you would to cout

    return os;
  }
};

Don't forget the friend keyword or you'll get the same error saying there are too many parameters.

Edward showed you exactly where to put it.

Yes, i did what you said in your first post, and then i got that error.

[ But you can make it even simpler like this:

struct gydytojas {
  int gydid, amzius, specialyb, telefonas, asmkod;
  String vardas[25];
  String pavarde[35];
  String adresas[50];

  friend ostream& operator<<(ostream& os, const gydytojas& obj)
  {
    // Print the members to os like you would to cout

    return os;
  }
};

Don't forget the friend keyword or you'll get the same error saying there are too many parameters.

Yep, that works! Thanks, just one question, after i write that info to that file, should that file would be empty? ;s

The file shouldn't be empty, but you should also actually be writing something in the overloaded operator. Replace the comment with code to write your struct's data members to os.

Ah..Could you suggest any example of syntax, because all, that I've tried was bad..

Below is an arbitrary example, hope it gives some insight

#include <fstream>
#include <string>
using namespace std;

class MyClass
{
	string 	strings[2];
	int 	value;
	friend ostream & operator << (ostream & stream, const MyClass & obj);

public:
	MyClass(const string & s1, const string & s2, const int n)
	{
		strings[0] = s1;
		strings[1] = s2;
		value = n;
	}
};

ostream & operator << (ostream & stream, const MyClass & obj)
{
    // because this overload is a friend of MyClass,
    // the access to private class members is possible here

    // stuff in the two strings + the integer value, separated by tabs
	stream
		<< obj.strings[0]
		<< "\t"
		<< obj.strings[1]
		<< "\t"
		<< obj.value
		<< "\n";

	return stream;
}

int main()
{
	MyClass test("String #1", "String #2", 123);

	ofstream ofs("c:\\temp\\o.txt");	
	ofs << test;

	return 0;
}

You probably need to overload the ostream operator << for AnsiString too.

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.