Search code examples
c++segmentation-faultaccess-violation

c++, reading string from file, segmentation fault


I have got segmentation fault and I don't know what's wrong. More then year ago I've wrote program which should write on hdd strings, integers and doubles included in my classes.

I've been always using this piece of code:

class Ttimes {
public:
  string name;
};

class R {
public:
  string name;
  vector<Ttimes> ttt;
};

vector<R>  rrr;
// while writing:

string file_name("C:\\abc\\example.ttt");
fstream f;
f.open(file_name.c_str(), ios::out | ios::binary);
if (f) {
  f.write((char *)&rrr.size(), sizeof(rrr.size()));

  for (int i = 0; i < rrr.size(); i++) {
    myString = rrr[i].name;

    int stringSize = myString.size();
    f.write((char *)&stringSize, sizeof(stringSize));
    f.write(myString.c_str(), myString.size());

    f.write((char *)&rrr[i].ttt.size(), sizeof(rrr[i].ttt.size()));

    for (int j = 0; j < rrr[i].ttt.size(); j++) {
      myString2 = rrr[i].ttt[j].name;

      int stringSize2 = myString2.size();

      f.write((char *)&stringSize2, sizeof(stringSize2));
      f.write(myString2.c_str(), myString2.size());
    } // end for j

  } // end for i

  f.close();
} // end if f

// while reading:

rrr.clear()

string file_name("C:\\abc\\example.ttt");
fstream f;
f.open(file_name.c_str(), ios::in | ios::binary);
if (f) {
  int howManyR;
  f.read((char *)&howManyR, sizeof(howManyR));

  for (int i = 0; i < howManyR; i++) {
    R empty;
    RRR.push_back(empty);
    int stringSize;
    string myString;

    f.read((char *)&stringSize, sizeof(stringSize));
    myString.resize(stringSize);

    f.read((char *)myString.c_str(), stringSize);

    rrr[i].name = myString;
    rrr[i].ttt.clear();

    Ttimes emptyT;

    int vectorTTTsize;
    r.read((char *)&vectorTTTsize, sizeof(vectorTTTsize));

    for (int j = 0; j < vectorTTTsize; j++) {
      rrr[i].ttt.push_back(emptyT);

      string myString2;
      int stringSize2;
      f.read((char *)&stringSize2, sizeof(stringSize2));
      myString2.resize(stringSize2);
      f.read((char *)myString2.c_str(), stringSize2);
      rrr[i].ttt[j].name = myString2;
    } // end for j

  } // end for i

  f.close();
}

Everything worked fine (for 1.5 of year!) but few days ago I've developed code - I've added another vector R and copy-paste the same write-read code. Since then from time to time I've got segmentation fault. When I try to use debbuger - it never shows the same line :/ I'm self-taught beginner and program for myself so please have mercy ;)


Solution

  • I've read Your answers, search in books and changed whole idea of reading-writing :) Now when I write I use:

    int stringSize=text.length()*sizeof(char);
    file.write(reinterpret_cast<char*>(&stringSize),sizeof(stringSize));
    file.write(text.c_str(),stringSize);
    

    And when I read I use:

    int stringSize=0;
    string text;
    plik.read(reinterpret_cast<char*>(&stringSize),sizeof(stringSize));
    char * tempCharArray = new char[stringSize+1]{};
    plik.read(tempCharArray,stringSize);
    text=tempCharArray;
    delete [] tempCharArray;
    

    No segmentation fault/access violation since then :) I guess reading "into" (char*)myString.c_str() was a problem, just as Chipster pointed out. Thanks.