0
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include <stdio.h> 
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <vector>
#include <sstream>
#define SHMSIZE 1024

using namespace std;

namespace patch
{
    template < typename T > std::string to_string( const T& n )
    {
        std::ostringstream stm ;
        stm << n ;
        return stm.str() ;
    }
}

struct process
{
    int r;
        string name;
        vector<string> lines;
};

int main(int argc, char * argv[])
{
     int firstRun = 1;  //Skipping First Line of Assign-1.ip.

        int quantum = 0;    //For taking input of quantum.
        int count = 0;      //For number of processes.
        int pchtoint;
        string c;
        char * pch;     //For tokenization.

        string reading_file;    //Reading a line from file.

        char * readarr;     //Converting "reading_file" to readarr for tokenization.

        process * p;

        //=== Quantum Input ===//
        cout<<"Enter Quantum size [1-1000]: ";
        cin>>quantum;

        while(quantum < 1 || quantum > 1000)
        {
              cout<<"Wrong input!!! Enter Again [1-1000]: "; 
              cin>>quantum;
        }
        //=====================//

        //===Filing===//
        ifstream read("Assign-2.ip");

        if(read.is_open())
        {
                while(!read.eof())
                {
                        getline(read, reading_file);

                        readarr = new char[reading_file.size() + 1];
                        for(int i = 0; i < reading_file.length(); i++)
                        {
                                readarr[i] = reading_file[i];
                        }

                        if(firstRun > 1)
                        {
                            int countingline = 0;           //counting the number of lines in a process.
                        pch = strtok (readarr," ,");

                        while (pch != NULL)
                        {
                            c = pch[1];
                            pchtoint = atoi(c.c_str());
                        p[pchtoint-1].r++;
                        p[pchtoint-1].lines.push_back(pch);
                        for(int i = 0; i < p[pchtoint-1].lines.size(); i++)
                            cout<<p[pchtoint-1].name<<"=="<<p[pchtoint-1].lines.at(i)<<endl;


                                    pch = strtok (NULL, " ,");     
                        }
                }
                else
                {
                    pch = strtok (readarr,",.-");
                        while (pch != NULL)
                        {   
                                count++;
                                pch = strtok (NULL, ",.-");  
                        }
                        p = new process[count];
                        string s = "p";
                        for(int i = 0; i < count; i++)
                {
                    s = s + patch::to_string(i+1);
                    p[i].name = s;
                    s = s[0];
                }
                            firstRun++;
                        }   
                }
        }
        else
        {
                cout<<"Cannot open file!!!"<<endl;
        }
        read.close();
        return 0;
}

Enter Quantum size [1-1000]: 2

p1==p1-l1

p2==p2-l1

p3==p3-l1

p1==p1-l1

p1==p1-l2

p2==p2-l1

p2==p2-l2

p3==p3-l1

p3==p3-l2

p1==p1-l1

p1==p1-l2

p1==p1-l3

p3==p3-l1

p3==p3-l2

p3==p3-l3

p1==p1-l1

p1==p1-l2

p1==p1-l3

p1==p1-l4

Segmentation fault (core dumped)

I am reading data from a cvs file. and storing it in struct that is p here. but I don't know why it is giving segmentation fault. I am compiling it on ubuntu terminal.

The input file contains data: P1, P2, P3,

p1-l1, p2-l1, p3-l1

p1-l2, p2-l2, p3-l2

p1-l3, , p3-l3

p1-l4, ,

melpomene
  • 79,257
  • 6
  • 70
  • 127
thirteen4054
  • 424
  • 3
  • 16
  • 1
    Use your debugger, we don't have your data file. – Richard Critten Mar 26 '17 at 17:14
  • I have attached the input. I don't know how to debug on terminal. – thirteen4054 Mar 26 '17 at 17:24
  • you're doing C with some C++. Use appropriate containers instead of error prone pointers, use nullptr instead of NULL and use constexpr over #define. Split your code into several functions. Also note that checking for EOF, reading and repeating is the wrong way, it should be reading, checking for errors (like EOF) and then repeat – The Techel Mar 26 '17 at 17:24
  • 1
    `while(!read.eof())` is wrong, see http://stackoverflow.com/q/5605125/1848654. – melpomene Mar 26 '17 at 17:25
  • Please read [mcve]. – melpomene Mar 26 '17 at 17:26
  • I changed while(getline(read, reading_file)) but still its segmentation fault. – thirteen4054 Mar 26 '17 at 17:34
  • I will try to keep in mind these guidelines in the future. and also thankyou for pointing out eof problem. Inside the while(getline(read, reading_file)) I put a check for reading_file.empty() and the problem was solved – thirteen4054 Mar 26 '17 at 17:49

0 Answers0