C++ 分段错误(核心转储),将字符*存储到结构的字符串向量 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #定义shmsize1024 使用名称空间std; 命名空间修补程序 { 模板st
分段错误(核心转储),将字符*存储到结构的字符串向量C++ 分段错误(核心转储),将字符*存储到结构的字符串向量 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #定义shmsize1024 使用名称空间std; 命名空间修补程序 { 模板st,c++,vector,struct,C++,Vector,Struct,分段错误(核心转储),将字符*存储到结构的字符串向量 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #定义shmsize1024 使用名称空间std; 命名空间修补程序 { 模板std::字符串到字符串(常量T&n) { std::ostringstream stm; stm(1000) { 香菇; } //=====================// //==归档===// ifstream读取(“Assign-2.i
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义shmsize1024
使用名称空间std;
命名空间修补程序
{
模板std::字符串到字符串(常量T&n)
{
std::ostringstream stm;
stm(1000)
{
香菇;
}
//=====================//
//==归档===//
ifstream读取(“Assign-2.ip”);
如果(读。是否打开())
{
而(!read.eof())
{
getline(读取、读取\u文件);
readarr=new char[reading_file.size()+1];
对于(int i=0;i1)
{
int countingline=0;//计算进程中的行数。
pch=strtok(readarr,“,”);
while(pch!=NULL)
{
c=pch[1];
pchtoint=atoi(c.c_str());
p[pchtoint-1].r++;
p[pchtoint-1]。行。推回(pch);
对于(int i=0;i使用你的数据调试器,我们没有你的数据文件。我已经附加了输入。我不知道如何在终端上调试。你正在用C++做C。使用适当的容器而不是容易出错的指针,使用Null pTR而不是NULL,并使用CONEXPROR来定义。nd重复是错误的方式,它应该是读取、检查错误(如EOF),然后重复,而(!read.EOF())
是错误的,请参阅。请阅读。
#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;
}