Answer the question
In order to leave comments, you need to log in
What causes a Segmentation fault (core dumped) error?
There is a class
class Car
{
public:
Car()
{
std::string Availability = "None";
std::string brand = "";
int carAge = 0;
};
Car( const std::string &avail, const std::string &brand, int carAge)
{
Availability = avail;
this->brand = brand;
this->carAge = carAge;
};
void Print()
{
std::cout << Availability << std::endl;
std::cout << brand << std::endl;
std::cout << carAge << std::endl;
}
private:
std::string Availability;
std::string brand;
int carAge;
};
int main()
{
std::string path = "flash.txt";
Car newcar("Yes", "Porsche", 5);
std::ofstream fout;
fout.open(path, std::ofstream::app);
if (!fout.is_open())
{
std::cout << "Ошибка. Не удалось открыть файл" << std::endl;
}
else
{
std::cout << "Удалось открыть файл. Операция успешна" << std::endl;
fout.write((char*)&newcar, sizeof(Car));
}
fout.close();
std::string path = "flash.txt";
std::ifstream fin;
fin.open(path);
if (!fin.is_open())
{
std::cout << "Не открылся файл"<< std::endl;
}
else
{
std::cout << "Файл открылся" << std::endl;
Car machine;
while (fin.read((char*)&machine, sizeof(Car)))
{
machine.Print();
}
}
fin.close();
}
Answer the question
In order to leave comments, you need to log in
gives a Segmentation fault (core dumped) error.
What is the reason for this error?
class Car { ... private: std::string Availability; std::string brand; int carAge; }; ... Car machine; while (fin.read((char*)&machine, sizeof(Car)))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question