Hi
I am trying to write a C++ program that will determine whether a string from a file; from the set of characters "[]" form a group. If so, I want to print the
text that is within the out most bracket group into another file. Some examples are:
xxxx[xxxx]xxxx = [xxxx]
[xxxx[xxxx]xxxx = [xxxx[xxxx]
[xxxx[xxxx]xxxx] = [xxxx[xxxx]xxxx]
x[x[x[xxx]xx]x]x]xx = [x[x[xxx]xx]x]
At the moment I can do the "xxxx[xxxx]xxxx = [xxxx]" example and the reading to and from files I am ok with. I am lost on the thought process I should
take in order to see if I have multiple brackets and whether they form a valid group.
Any help would be great.
Currently I have:
Code:
#include<iostream>
#include<fstream>
#include<cstdio>
#include<cstring>
using namespace std;
#define OPEN '['
#define CLOSE ']'
int main()
{
ifstream inData;
ofstream outData;
char filenameIn[100];
char filenameOut[100];
char lineOfText[200];
char chBuffer[BUFSIZ];
char *Start;
char *End;
int nChar;
cout << "Enter filename to read DATA from: ";
cin >> filenameIn;
cout << endl;
inData.open(filenameIn);
cout << "Enter filename to write DATA to: ";
cin >> filenameOut;
cout << endl;
outData.open(filenameOut);
if(!inData)
cout << "ERROR in opening " << filenameIn << endl;
else
if(inData)
{
cout << "Opening file: " << filenameIn << endl << endl;
inData.get(lineOfText, 200);
cout << lineOfText << endl << endl;
if (((Start = strchr(lineOfText, OPEN)) != '\0') && (((End = strchr(Start, CLOSE))) != '\0'))
{
nChar = End - Start + 1;
strncpy(chBuffer, Start, nChar);
chBuffer[nChar] = '\0';
cout << "The characters found are: " << chBuffer << endl << endl;
outData << chBuffer << endl;
}
else
if(!(Start && End))
cout << "None found" << endl << endl;
}
inData.close();
outData.close();
cout << endl << endl << endl;
return 0;
}