(read is reading a file, the R's are expressions to evaluate, write is writing to a file)
and I need my program to read each line, perform the specified command, then move on to the next.
One suggestion I had was to remove all symbols, except for '.', then store the results in a stringstream. If I did that, then maybe I could make if statements that check the first word in the stringstream, and perform that action.
I just can't figure this out because I will not know the names of the input or the output files in the program and I'm not sure how to check each individual word in a string stream.
If anyone could help me with this I would be very appreciative.
you can iterate a string with [] and check each character. You can also search for specific characters. If you make the format friendly (consider, spaces or commas or other symbols between things?) you can break it up easier.
I mean you know file names are between ' ' symbols... you can look for ' as one part of your code.
It's usually painful to handle languages with no actual definition - especially when you're trying to define them in code without any plan.
The first step is to define your language's grammar. This will help you determine how sophisticated a solution is required, and it will help you translate the definition into code. I recommend posting the result here when you're done.
ifstream Script("Script.txt");
string line, word;
ifstream files[100];
int j = 0;
int names[100];
while(getline(Script, line))
{
if(line[0] == 'r' && line[1] == 'e' && line[2] == 'a' && line[3] == 'd')
line = Format(line);
elseif (line[0] == 'w' && line[1] == 'r' && line[2] == 'i' && line[3] == 't' && line[4] == 'e')
line = Format(line);
stringstream ss(line);
ss.seekg(0,ios::end);
int size = ss.tellg();
ss.seekg(0,ios::beg);
string words[size];
int i = 0;
while (ss >> word)
{
words[i] = word;
i++;
}
if(words[0] == "read")
{
string fileName = words[2];
cout << "File name: " << words[2] << " j: " << j << endl;
files[j].open(fileName.c_str());
if(!files[j])
cout << "FILE ERROR!" << endl;
string test;
while(getline(files[j], test)) //This part was just to make sure that it was opening the right
cout << test << endl;//files
++j;
}
}
This successfully opens the correct files and displays them. The part I am confused about now is how I would make it where I can designate a name to each file. So when I try to do a+b, it knows to use a,txt and b.txt.
Maybe if the expression is R1 = a + b I could just tell it to add ".txt" to a and b, then do the operation? Does that sound right, or is there a better way to do it?
It isn't about better ways to do a specific task. Its more about the whole design and approach... what exactly do you want your script language to support? If its just like 4 or 5 simple commands, yea that would work and its fine.