7 Replies - 12913 Views - Last Post: 08 August 2009 - 02:00 PM Rate Topic: -----

#1 nathanpc   User is offline

  • SCIENCE!
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Get a String That is Inside Quotes

Post icon  Posted 07 August 2009 - 04:17 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Get a String That is Inside Quotes

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Get a String That is Inside Quotes

Posted 07 August 2009 - 06:20 PM

After you input all of the file, search through it for key words such as, (in this case) copy_input with indexOf. Then after that has completed do a search for a segment between " and ". This should return the segments between quotes.

string file = get file;
int start = file.indexOf(copy_input); 
int end = file.indexOf("\n", start);

string line = file.substring(start, end);
int quoteStart = line.indexOf("\"");
int quoteEnd = line.indexOf("\"", quoteStart);

string quotes = line.substring(quoteStart, quoteEnd)


This post has been edited by Dogstopper: 07 August 2009 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 nathanpc   User is offline

  • SCIENCE!
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Get a String That is Inside Quotes

Posted 08 August 2009 - 08:43 AM

Hello Dogstopper,
Thanks for answer me, but now i'm getting errors when compiling, and here is the modified code:
    if (linha.find("copy_input") != string::npos)
       {
          string file = get file;
          int start = file.indexOf(copy_input);
          int end = file.indexOf("\n", start);

          string line = file.substring(start, end);
          int quoteStart = line.indexOf("\"");
          int quoteEnd = line.indexOf("\"", quoteStart);

          string quotes = line.substring(quoteStart, quoteEnd);

          cout << quotes << endl;
       }

If you need the console log iit is here, but i can't post it here, then i've uploaded it:
http://nathanpc.pastebin.com/f11f88992

Can you help me?

Thanks,
Nathan Paulino Campos

This post has been edited by nathanpc: 08 August 2009 - 08:48 AM

Was This Post Helpful? 0
  • +
  • -

#4 nathanpc   User is offline

  • SCIENCE!
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Get a String That is Inside Quotes

Posted 08 August 2009 - 12:36 PM

Someone can help me with this errors?
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Get a String That is Inside Quotes

Posted 08 August 2009 - 12:45 PM

Yes, I know exactly what is wrong. I think... Are you using a string or a char** (char array), because if you are, let me know and i can help.

~ Dog
Was This Post Helpful? 0
  • +
  • -

#6 nathanpc   User is offline

  • SCIENCE!
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Get a String That is Inside Quotes

Posted 08 August 2009 - 12:50 PM

Hello Dogstopper,
I'm using a string, but see here what is happening, the console error, i can't post it here, then i pasted it in Paste Bin: http://nathanpc.pastebin.com/f11f88992.

Thanks,
Nathan Paulino Campos
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Get a String That is Inside Quotes

Posted 08 August 2009 - 12:56 PM

Oh dang...Im sorry...I accidentally switched back to Java in my brain. Here is a list of potential operators that can help you.

http://www.cplusplus.../string/string/

From there, just find the proper methods. Once again, sorry for the confusion

use the find and substr methods

This post has been edited by Dogstopper: 08 August 2009 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

#8 nathanpc   User is offline

  • SCIENCE!
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Get a String That is Inside Quotes

Posted 08 August 2009 - 02:00 PM

Ok, thanks!
+1 Thanked!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1