views:

215

answers:

4

Hello,

I'm building a simple interpreter of a language that i'm developing, but how i can do a cout of something that is after a word and in rounded by "", like this:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main( int argc, char* argv[] )
{

 if(argc != 2)
 {
    cout << "Error syntax is incorrect!\nSyntax: " << argv[ 0 ] << " <file>\n";
   return 0;
 }
 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 == "print")
   {
   cout << text after print;
   }
 }
  return 0;
}

And how i can remove the "" when printing the text. Here is the file example:

print "Hello, World"

Read my post in the middle of the answers!

Thanks

A: 

I don't understand your problem. On input of

print "Hello, World"

your test of linha == "print" will never be true (as linha contains the rest of the line so the equalitry is never true).

Are you looking for help on string processing, i.e. splitting of the input line?

Or are you looking for regular expression help? There are libraries you can use for the latter.

Dirk Eddelbuettel
I'm building my own interpreter, the i want to do this: Show only the content that is in the "" on linha, because it's my language that i'm developing. Thanks!
Nathan Campos
+2  A: 

I hope this simple example would help.

std::string code = " print \" hi \" ";
std::string::size_type beg = code.find("\"");
std::string::size_type end = code.find("\"", beg+1);

// end-beg-1 = the length of the string between ""
std::cout << code.substr(beg+1, end-beg-1);

This code finds the first occurnce of ". Then finds the next occurrence of it after the first one. Finally, it extracts the desired string between "" and prints it.

AraK
Haven't tested it, but it's clearly a simple way to go that just works.
Benoît
+1  A: 

I'm assuming what you want is to identify quoted strings in the file, and print them without the quotes. If so, the below snippet should do the trick.

This goes in your while(!file.eof()) loop:

string linha;
while(!file.eof())
{
    getline(file, linha);
    string::size_type idx = linha.find("\""); //find the first quote on the line
    while ( idx != string::npos ) {
     string::size_type 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:" << quotes.substr(1,quotes.length()-2) << endl;

     //check for another quote on the same line
     idx = linha.find("\"",idx_end+1); 
    }  
}
nagul
Thanks nagul, you helped me very much!
Nathan Campos
A: 

Hello AraK,

Thanks for your answer, but now when i try to compile i receive this error:

ubuntu@ubuntu-laptop:~/Desktop/Tree$ g++ -o tree tree.cpp
tree.cpp: In function ‘int main(int, char**)’:
tree.cpp:48: error: expected initializer before ‘beg’
tree.cpp:49: error: expected initializer before ‘end’
tree.cpp:52: error: ‘beg’ was not declared in this scope
tree.cpp:52: error: ‘end’ was not declared in this scope
ubuntu@ubuntu-laptop:~/Desktop/Tree$

What is wrong?

I'm going to delete this question when i solve this problem, because i know that here is for answers not for questions.

Nathan Campos
post your code.
Ben
Manpages say that find returns size_t, I couldn't find anything about string::size_type on cplusplus.com You might try size_t instead.
pmr
std::string::size_type is a type defined inside the std::string class, it should work.
Ben
@prm thanks very much, now it compiles, but please do this comment in the @AraK post, bacause i have to delete this post.
Nathan Campos
Because they will down vote me more, but i said in this post that i will delete it.
Nathan Campos