A
A
Alexander Markelov2022-03-14 14:01:03
C++ / C#
Alexander Markelov, 2022-03-14 14:01:03

How to solve the Segmentation Fault problem?

I have 50 lines of information about books that I want to parse into an array of Book class objects, when I try to do this, it throws a segmention fault

result.txt:
101 Humphrey Cloak_&_Dagger 2010 14429
199 Helix White_of_the_Eye 1996 7490
132 Rydeard Claire_Dolan 2005 21594
110 Corston Resident_Evil:_Apocalypse 2006 6359
111 Burwell American_Boy:_A_Profile_of:_Steven_Prince 1994 18415
114 Coetzee The_Count_of_Monte_Cristo 1988 17376
136 Ruslin Broken_City 1996 17312
125 Stoll I_Like_Killing_Flies 2004 19781
119 Dany Underworld:_Evolution 2000 12234
145 Cordingly Flight 2003 12644
102 Rosander Revenge_of_the_Ninja 2005 7779
100 Simko Shrine 1995 20235
131 Snazel Number_One_with_a_Bullet 2011 26443
147 Boshell Like_Dandelion_Dust 1996 26886
174 Sherme Le_crocodile_du_Botswanga 2006 21934
116 Duggan Madhouse 2007 32725
198 Hadaway War_and_Peace 1998 7827
108 Symondson Me_and_Earl_and_the_Dying_Girl 2011 19963
165 Grayshan The_Boy 1994 9853
120 Simson Cheech_and_Chong's_Up_in_Smoke 1990 28560
122 Shewan Pope_Joan_(Die_Päpstin) 1999 28557
133 Jantel Mr._Moto_Takes_a_Chance 1992 14269
195 Fenge Deathwatch 2001 29984
187 Meagher Take_Me_Home_Tonight 2008 7489
112 Siegertsz Happiest_Girl_in_the_World 2010 24411
126 Baythrop Beau_Pere 2004 25498
117 Yashunin Munyurangabo 2000 20964
121 Piatti Grey_Gardens 1966 27347
177 Hasard If_Footmen_Tire_You_What_Will_Horses_Do? 1997 5392
166 Bogays Wu_Tang_Master 1998 15531
181 Oswick The_Hearse 1996 13644
112 Scutchings Torn_Curtain 1992 19821
144 Canby Johnny_Guitar 2007 23021
165 De_Bell Fresh_Bait 2009 2470
137 Delf Man_Who_Loved_Cat_Dancing 2000 26181
105 Fortun Anger_Management 2008 8668
104 Darinton Bound_for_Glory 1994 19846
106 Mangam March_of_the_Wooden_Soldiers 2000 10725
195 Ceci Snow_White_and_the_Seven_Dwarfs 2005 28449
140 Harget Laurence_Anyways 2000 32080
154 Hopewell Fire_in_the_Blood 2002 23370
178 Yatman Shuttle 1996 3460
193 Allans My_Dear_Secretary 2012 19547
141 Santoro Hara-Kiri:_Death_of_a_Samurai 1997 17045
145 Leathem Earrings_of_Madame_de... 1997 30346
149 Blumsom Shackleton's_Antarctic_Adventure 2012 8713
199 McEachern Merlusse 1994 17002
188 Trayes Born_American 2005 5406
173 Kermannes Yesterday 1994 28676
150 Grimm All_the_Way_Home 2009 26855


#include <iostream>
#include <fstream>
#define N 50
using namespace std;

class Book
{
private:
  int id;
  string author;
  string name;
  int year;
  long int price;
public:
  Book()
  {
    id = 0;
    author = "---";
    name = "---";
    year = 0;
    price = 0;
  }

  Book(int id, string author, string name, int year, long int price)
  {
    this->id = id;
    this->author = author;
    this->name = name;
    this->year = year;
    this->price = price;
  }

  int getId()
  {
    return id;
  }

  string getAuthor()
  {
    return author;
  }

  string getName()
  {
    return name;
  }

  int getYear()
  {
    return year;
  }

  int getPrice()
  {
    return price;
  }
  void getAll()
  {
    cout << "Id:" << id << "\t" << "Author:" << author << "\t" << "Name:" << name << "\t" << "Year:" << year << "\t" << "Price:" << price << endl;
  }
};



int main()
{

  setlocale(LC_ALL, "ru");
  string path = "result.txt";
  ifstream fin;
  fin.open(path);
  string str1,str2,str3,str4,str5;
  int t = 0;
  Book A[N];
  if (!fin.is_open())
  {
    cout << "Ошибка открытия файла" << endl;
  }
  else
  {
    while (!fin.eof())
    {
      fin >> str1;
      fin >> str2;
      fin >> str3;
      fin >> str4;
      fin >> str5;
      A[t] = Book(stoi(str1), str2, str3, stoi(str4), stoi(str5));
      t++;
    }
  }
  for (int i = 0; i < N; i++)
  {
    A[i].getAll();
  }
  fin.close();
  return 0;
}

gdb result:
622f206ac6aaa704083228.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2022-03-14
@wataru

Segmentation fault is a memory error. Or going beyond the bounds of the array, or something is wrong with the pointers.
I suspect that you don't have 50 lines in the file (I'm too lazy to count). So you get out of the array boundary.
Well, or there are some extra line breaks, there are spaces. so fin.eof() is not executed after the last line is read. Then you try to read something else, but you get back an empty string. This would not be a problem, but then you try to cram it all into the 51st element of the array.
I advise you to use std::vector instead of define N and a static array. In the file read loop, push_back the new object into the vector (better yet, emplace_back to save unnecessary copying).
When outputting, take the size() of your vector.
If you notice an extra book at the end with empty data in the output, then you can add an input check - if at least one of the 5 lines is empty, then you do not need to create a new book.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question