struct gydytojas {
   int gydid, amzius, specialyb, telefonas, asmkod;
   String vardas;
   String pavarde;
   String adresas;
  friend ostream& operator<<(ostream& os, const gydytojas& obj)
    {
         os << obj.vardas.c_str()
            << obj.pavarde.c_str()
            << obj.adresas.c_str()
            << obj.gydid
            << obj.amzius
            << obj.specialyb
            << obj.telefonas
            << obj.asmkod;

         // Print the members to os like you would to cout

          return os;
        };
  friend fstream& operator>>(fstream& fs, const gydytojas& obj)
   {
        fs >> obj.vardas.c_str();
           >> obj.pavarde.c_str();
           >> obj.adresas.c_str();
           >> obj.gydid;
           >> obj.amzius;
           >> obj.specialyb;
           >> obj.telefonas;
           >> obj.asmkod;
      // reading code

      return fs;
     };

  };

gydytojas gydmas[100];

This code is in unit1.h

gydytojas pgyd;
pgyd.vardas=Edit21->Text;
pgyd.pavarde=Edit22->Text;
pgyd.adresas=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;
bool pagalb = false;

int j=1;

fstream kd("Gydytojai.sed");
while (!kd.eof())      // Checking if the entry already exist;
 { if (gydmas[j].vardas==pgyd.vardas) {ShowMessage(L"Gydytojo id jau egzistuoja");
                                        pagalb=true;};      // if the entry exist, variable pagalb becomes true
   j++;};
kd.close();
ofstream fr("Gydytojai.sed");
 if (pagalb==false)  {gydmas[j+1]=pgyd; fr << gydmas[j+1];};
 // if variable pagalb false,  then the structure gydytojas is written to file as array
fr.close();

This code is unit1.cpp, as you see, im trying to search the exact id from the fields, which are written, but now i got the project error message. Before that, gydytojas.vardas field hasn't been recognized in code, so all the information was always written to file. Any suggestion why i get error message, and what way would be better?

Recommended Answers

All 9 Replies

What's the error message?

The error meesage says, that : "Access violation at address 0042FEA3 in mode 'Project2.exe'.Read of address 00000026

lines 21-30. The operator >>. When inputting std::string you don't use the c_str() method. There's how to code that

friend fstream& operator>>(fstream& fs, const gydytojas& obj)
   {
        fs >> obj.vardas;
           >> obj.pavarde;
           >> obj.adresas;
           >> obj.gydid;
           >> obj.amzius;
           >> obj.specialyb;
           >> obj.telefonas;
           >> obj.asmkod;
      // reading code

      return fs;
     };

lines 21-30. The operator >>. When inputting std::string you don't use the c_str() method. There's how to code that

friend fstream& operator>>(fstream& fs, const gydytojas& obj)
   {
        fs >> obj.vardas;
           >> obj.pavarde;
           >> obj.adresas;
           >> obj.gydid;
           >> obj.amzius;
           >> obj.specialyb;
           >> obj.telefonas;
           >> obj.asmkod;
      // reading code

      return fs;
     };

Yes, i made with that thing before putting c_str(), but if i did that i get an error in code: [C++ Error] Unit1.h(228): E2094 'operator>>' not implemented in type 'fstream' for arguments of type 'AnsiString'

what is AnsiScring ? Why are you not using normal std::string ? If AnsiString is something you created yourself then you need to write an overloaded >> operator for it so that you can use it with fstream. If you can't do that then use std::stream and assign AnsiString to it. You could also use c style character arrays.

Also -- remove the semicolons at the end of each of those lines.

friend fstream& operator>>(fstream& fs, const gydytojas& obj)
   {
      std::string vardas;
      std::string pavarde;
      std::string adresas;
        fs >> vardas   // <<<<< remove this semicolon
           >> pavarde   // <<<<< remove this semicolon
           >> adresas   // <<<<< remove this semicolon

           >> obj.gydid  // <<<<< remove this semicolon
           >> obj.amzius  // <<<<< remove this semicolon
           >> obj.specialyb  // <<<<< remove this semicolon
           >> obj.telefonas  // <<<<< remove this semicolon
           >> obj.asmkod;
      // reading code

      obj.vardas = vardas;
      obj.pavarde = pavarde;
      obj.adresas = adresas;
      
      return fs;
     };

obj must NOT be const because you intend to modify it, so use only plain
gydytojas& obj i.e. friend fstream& operator>>(fstream& fs, gydytojas& obj)

If gydytojas is const or not, i get this error:
[C++ Error] Unit1.h(232): E2285 Could not find a match for 'AnsiString::operator =(string)'
Full parser context
Unit1.cpp(10): #include D:\Programavimas\Galutinis\Unit1.h
Unit1.h(19): class TForm1
Unit1.h(276): decision to instantiate: fstream & operator >>(fstream &,TForm1::gydytojas &)
--- Resetting parser context for instantiation...
Unit1.h(227): parsing: fstream & operator >>(fstream &,TForm1::gydytojas &)

I take it comes from the following

obj.vardas = vardas;
obj.pavarde = pavarde;
obj.adresas = adresas;

You can workaround this by

// pass a const char * to AnsiString::operator =
obj.vardas = vardas.c_str();  
obj.pavarde = pavarde.c_str();
obj.adresas = adresas.c_str();

Ah, Hadn't any time those days, today i made all changes, but i get the same project error, if i i'm trying to write into file this thing..

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.