Hello,
I'm learning C++ and all that i learn i try to put in a interpreter of a language that i'm developing, it's only to practice, but here is a thing that i don't learned already, that is how i can get a string that is inside quotes, because i want to build a copy function in my language, here is the source of my interpreter:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main( int argc, char* argv[] )
{
    // Error Messages
    string extension = argv[ 1 ];
    
    if(argc != 2)
    {
       cout << "Error syntax is incorrect!\nSyntax: " << argv[ 0 ] << " <file>\n";
      return 0;
    }
    if(extension[extension.length()-3] != '.')
    {
       cout << "Extension not valid!" << endl;
       cout << "Default extension *.tr" << endl;
      return 0;
    }
    if(extension[extension.length()-2] != 't')
    {
       cout << "Extension not valid!" << endl;
       cout << "Default extension *.tr" << endl;
      return 0;
    }
    if(extension[extension.length()-1] != 'r')
    {
       cout << "Extension not valid!" << endl;
       cout << "Default extension *.tr" << endl;
      return 0;
    }
    // End of the error messages
    
    ifstream file(argv[ 1 ]);
    if (!file.good()) {
       cout << "File " << argv[ 1 ] << " does not exist.\n";
      return 0;
    }
    
    string linha;
    while (!file.eof())
    {
    getline(file, linha);
    if (linha.find("print") != pos)
        {
           size_t idx = linha.find("\""); //find the first quote on the line
           while ( idx != pos )
              {
                 size_t idx_end = linha.find("\"",idx+1); //end of quote
                 string quotes;
                 
                 quotes.assign(linha,idx,idx_end-idx+1);
                 // do not print the start and end " strings
                 cout << quotes.substr(1,quotes.length()-2) << endl;
                 //check for another quote on the same line
                 idx = linha.find("\"",idx_end+1);
              }
       }
    if (linha.find("copy_input") != pos)
       {
          size_t idx = linha.find("\""); //find the first quote on the line
          while ( idx != pos )
             {
                size_t idx_end = linha.find("\"",idx+1); //end of quote
                string quotes;
                char* inputFile;
                
                ifstream file_input( inputFile );
                quotes.assign(linha,idx,idx_end-idx+1);
                
                Here is the code
                
                idx = linha.find("\"",idx_end+1);
             }
       }
    }
  return 0;
}

Here is a template that i use for copying files:

#include <iostream>
#include <fstream>
using namespace std;

int main( int argc, char* argv[] )
{   
    char* inputFile;
    char* outputFile;
    inputFile = argv[ 1 ];
    outputFile = argv[ 2 ];
    
    ifstream in( inputFile );
    if( in.is_open() )
       {
          ofstream out( outputFile );
          out << in.rdbuf();
       }
  return 0;
}

And here is the syntax of my copy function in my language:

copy_input "The String Will Be Here"

Thanks,
Nathan Paulino Campos

You posted code, which is good, but didn't tell us whether the code works or what the input file should be, how to use your program, which part of the code to concentrate on, stuff like that.

I'm not entirely sure what you want, but I think that this is a line in input file:

copy_input "The String Will Be Here"

and which you'll read in and store as a string. The you want to extract this part:

The String Will Be Here

Set up a function that does this:

string ExtractSubstring (string longstring)
// longString is a string that contains beginning and ending quotes.
// return value: the substring within longString that is between the two quotes.

So find the indexes of the two quotes

0         1         2         3
0123456789012345678901234567890123456789
copy_input "The String Will Be Here"

In this case it's 11 and 35, so return the substring from 12 to 34.

The find and substr functions from the string library can come in handy here.

Hello VernonDozier,
That syntax is in my language that i'm developing.

Thanks,
Nathan Paulino Campos

You can use the indexes, but you can also use flags.

First try to read in a file. Then check for the first quote ( " ) and
if found read in the data until another quote ( " ) is found.

Hello VernonDozier,
That syntax is in my language that i'm developing.

Thanks,
Nathan Paulino Campos

Right, but I am assuming you are developing a function along the lines of what I suggested and you are having trouble. I didn't see that function in the code you posted and I didn't know what worked and didn't work in the code and where you wanted us to look, stuff like that. And I'm still just assuming that you are having problems writing a function along the lines of what I suggested. You haven't actually said that. So what precisely is the question?

But how i can do this?

We cross-posted. Does "this" refer to the function I am suggesting or do you want to read in from the file and keep track of quotes as you're reading in, which seems to possibly be the case from your earlier code, and hence there's no string to parse?

I want to read the thing that are in the quotes and use this thing that is inside the quotes as a variable(char*), only this.

I want to read the thing that are in the quotes and use this thing that is inside the quotes as a variable(char*), only this.

Read from the file a line at a time using getline, then extract the string using the function I suggested in post number 2.

ifstream ins;
// initialize ins with filename.
string wholeLine;
getline (ins, wholeLine, '\n');
string partInQuotes = ExtractSubstring (wholeLine);

You'll need to write the function. use find and substr from string to do so.

Read from the file a line at a time using getline, then extract the string using the function I suggested in post number 2.

ifstream ins;
// initialize ins with filename.
string wholeLine;
getline (ins, wholeLine, '\n');
string partInQuotes = ExtractSubstring (wholeLine);

You'll need to write the function. use find and substr from string to do so.

Didn't notice you wanted a char*, not a string to be returned. Same idea though. You can change a string to a char* and vice-versa. Use c_str ().

http://www.cplusplus.com/reference/string/string/c_str/

Thanks, for the tips and the resource!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.