I am basically trying to write some values from different files to a text file.
I am getting my output printed on the terminal but its unable to write to a file. There is a problem in writing to pfile
. Otherwise this code works fine.
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
char word[20];
ifstream ifile;
ofstream pfile;
string iline, event_id, event_id_modified, Event, pline,s;
int found, i=1, c_word = 0, c_line =0;
float comm_pos;
ifile.open("tryrsvp.doclist",ios::in);
pfile.open("polarityy.txt",ios::app);
pfile<<"event_id"<<"\t"<<"polarity"<<"\n";
while(!ifile.eof())
{
c_word =0;
c_line =1;
ifstream dfile;
getline(ifile,iline);
found = iline.find('\n');
Event = iline.substr(15,iline.size()-4-15);
event_id = iline.substr(0,found-1);
if(event_id.size()==0)
break;
event_id_modified = event_id;
event_id_modified.append("_auto_anns/exp_polarity.txt");
char *distributed_file = new char[event_id_modified.size()+1];
copy(event_id_modified.begin(), event_id_modified.end(), distributed_file);
distributed_file[event_id_modified.size()] = '\0';
dfile.open(distributed_file,ios::in);
strcpy(word,"positive");
while (dfile >> s)
{
if(s == word)
++ c_word;
c_line++;
}
c_line=c_line/2;
if(c_word==0)
comm_pos = 0;
else
comm_pos = (float)(c_word)/(float)(c_line);
cout<<event_id<<"\t";
cout<<c_word<<"\t"<<c_line<<"\t"<<comm_pos<<"\n";
pfile<<Event<<"\t"<<comm_pos<<"\n";
dfile.close();
delete[] distributed_file;
if(ifile.eof())
break;
}
pfile.close();
ifile.close();
}
I am getting this error
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
Aborted (core dumped)