Advertisement
Maco153

StudentFile.cpp

Mar 21st, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include "StudentFile.h"
  2.  
  3.  
  4. StudentFile:: StudentFile(){
  5.  
  6.     mtx=NULL;
  7. }
  8. StudentFile::StudentFile(StudentList * p_studentList,const char * filename){
  9.     cout<<"From the constructor\n";
  10.     student_file.open(filename,ios::in);
  11.     studentList = p_studentList;
  12.    
  13. }
  14.  
  15. void StudentFile:: set_mutex(std::mutex* my){
  16.     cout<<"hello form the mutex \n";
  17.     mtx=my;
  18.     cout<<"hello form the mutex \n";
  19.  
  20. }
  21.  
  22. int StudentFile::processFile(StudentFile* name){
  23. cout<<"from the processFile begin\n";
  24.     //std::lock_guard <std::mutex> lock (name-> *mtx);
  25.    
  26.    
  27.     int processed_students = 0;
  28.     if ( !student_file.is_open()) return processed_students;
  29.     string line="";
  30.     string id = "";
  31.     string fname="";
  32.     string mname="";
  33.     string lname="";
  34.     string gpa="";
  35. cout<<"from before the locking\n";
  36.     name->mtx->lock();
  37.     while(getline(student_file,line)) {
  38.         stringstream iss(line);
  39.         getline(iss, id, ',');
  40.         getline(iss, fname, ',');
  41.         getline(iss, mname, ',');
  42.         getline(iss, lname, ',');
  43.         getline(iss, gpa, ',');
  44.    
  45.         *studentList += new Student(atol(id.c_str()),
  46.                         fname.c_str(),
  47.                         mname.c_str(),
  48.                         lname.c_str(),
  49.                         atof(gpa.c_str()));
  50.         processed_students++;
  51.     }
  52.    
  53.    
  54. name->mtx->unlock();
  55. cout<<"From after the unlocking\n";
  56.     return processed_students;
  57.    
  58.  
  59. }
  60. int StudentFile::startThread(StudentFile * me)  { return me->processFile(me); }
  61. StudentFile::~StudentFile() {
  62.  
  63.     if ( student_file.is_open()) {
  64.        
  65.         student_file.close();
  66.  
  67.     }
  68.    
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement