The code below is giving me the SIGSEGV error, segmentation fault at line number 66(found using debugger).According to me everything is fine but still don't know what is done wrong by me. Please someone help me out to sort the problem
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "video.h"
#include "Categories.h"
using namespace std;
string Video::selected_category(int category_num) {
return category_array[category_num];
}
void Video::init_cate_array() {
}
int Video::total_objects_video(string filename) {
Video temp_obj;
int totalobjects = 0;
int totalsize = 0;
fstream file(filename.c_str(),ios::in);
file.seekg(0,ios::end);
totalsize = file.tellg();
totalobjects = totalsize - sizeof(temp_obj);
file.close();
return totalobjects;
}
int Video::get_id(string category) {
Video temp_obj;
int id_counter;
int totalobjects = 0;
string filename = category+".dat";
fstream file(filename.c_str(),ios::in);
if(file == NULL) {
file.close();
return 1; /// If the file doesnot existss...
} else {
if(file.is_open()) {
totalobjects = total_objects_video(filename);
for(int i =0 ; i<totalobjects; i++) {
file.read((char *)&temp_obj , sizeof(temp_obj));
id_counter = temp_obj.video_id;
}
}
++id_counter;
file.close();
return id_counter;
}
}
void Video::display_data() {
cout<<"\n Video Id = "<<video_id<<endl;
cout<<"\n Category_name = "<<category_name<<endl;
cout<<"\n Category_short_name = "<<category_short_name<<endl;
cout<<"\n Video name is "<<name<<endl;
cout<<"\n Video size is "<<video_size<<endl;
cout<<"\n Video length is "<<length;
}
//// In this category_name is passed with the function.
void Video::set_data(string category) {
issued = false;
video_id = get_id(category);
category_name = category;
category_short_name = category.substr(0,3);
cout<<"\n Category short_name is "<<category_short_name<<endl;
cout<<"\n The category name of the object is "<<endl;
cout<<"\nEnter the name of the video "<<endl;
cin>>name;
cin.ignore();
cout<<"\n Enter the video size in MiB "<<endl;
cin>>video_size; /// Size must be in MB
cout<<"\n Enter the length of video "<<endl;
cin>>length;
}
void Video::add_video() {
string choosen_category;
int category_num;
string filename;
string *ptr1;
// string category_array[30];
//init_cate_array();
Video temp;
Categories obj;
obj.view_categories();
//ptr1 = obj.store_categories();
//// This portion will print the data read from the file and store it in
// a local array.
int counter = 0;
for(int i = 0;i < 30; i++) {
category_array[i] = "";
}
fstream file("catgories.dat",ios::in);
if(file.is_open()) {
while(getline(file,category_name)) {
category_array[counter] = category_name;
cout<<"\n Data is "<<category_array[counter];
++counter;
}
}
file.close();
do {
cout<<"\n Select the category number you want to add video in ? "<<endl;
cin>>category_num;
filename = selected_category(category_num);
if(filename == "") {
cout<<"\n Not a category,try again ";
}
}while(filename == "");
choosen_category = filename;
filename = filename+".dat";
cout <<"\n Filename is "<<filename;
temp.set_data(choosen_category);
temp.display_data();
//// opening the filename entered by the user.
// fstream fileOut(filename.c_str(),ios::app);
//
// if(fileOut.is_open()) {
// fileOut.write((char *)&temp,sizeof(temp));
// fileOut.close();
// cout<<" \n\n\nVideo record added :)";
// temp.display();
// } else {
// fileOut.close();
// fstream fileOut(filename.c_str(),ios::out);
// fileOut.write((char *)&temp,sizeof(temp));
// fileOut.close();
// cout<<" \n\n\nVideo record added :)";
// temp.display();
// }
//
}