Code for extraction of word and links from the string
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
void parsefunc(ifstream& ifile, ofstream& ofile)
{
string input;
//vector <char> input;
//bool recordingbracket = false;
string linkname;
while (ifile.fail() == false) // as long as there is an input
{
char ab = ifile.get();
if (ifile.fail() == true)
{
break;
}
// cout<<ab;
if ((ab >= 65 && ab<= 90) || (ab >= 97 && ab <= 122))
{
// input.push_back(ab);
stringstream chr;
chr<<ab; // char variable to a string
input = input + ab;
}
else if (ab == ' ')
{
if (input[input.size()-1] != '\n' )
{
// input.push_back('\n');
input = input + '\n';
}
}
else if (ab== '(' || ab== '[' )
{
// input.push_back("LINK");
if ( ab == '(')
{
input = input + "LINK (";
string link;
while (ifile.fail() == false)
{
char abb = ifile.get();
if (abb !=')')
{
stringstream abs;
abs<<abb;
link = link + abb;
}
else
{
break;
}
}
if(linkname == "")
{
linkname = link;
}
input = input + link+ "," +linkname + ")";
}
else {
linkname = "";
while (ifile.fail() == false)
{
char abb = ifile.get();
if (abb !=']')
{
stringstream abs;
abs<<abb;
linkname = linkname + abb;
}
else
{
abb = ifile.get();
if( abb != '(')
{
input = input + linkname;
break;
}
else
{
input = input + "LINK (";
string link;
while (ifile.fail() == false)
{
char abb = ifile.get();
if (abb !=')')
{
stringstream abs;
abs<<abb;
link = link + abb;
}
else
{
break;
}
}
if(linkname == "")
{
linkname = link;
}
input = input + link+ "," +linkname + ")";
}
break;
}
}
}
}
}
/* for (int i =0; i< input.size(); i++)
{
cout<<input[i];
} */
//cout<<input;
ofile<<input;
}
int main(int argc , char* argv[])
{
if(argc < 3 ){
cerr << "Please provide an input and output file" << endl;
return 1;
}
ofstream ofile(argv[2]);
ifstream ifile(argv[1]);
//int len = atoi(argv[1]);
parsefunc(ifile, ofile);
ofile.close();
ifile.close();
return 0;
}
This code is not working for all cases. Anyone kindly help me out.