Thread: Read Something After a Word

  1. #1
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72

    Exclamation Read Something After a Word

    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:
    Code:
    #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:
    Code:
    print "Hello, World"
    Thanks,
    Nathan Paulino Campos
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: [email protected]
    ICQ: 424738586
    AIM: nathanjava

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Read this, it'll make your task a lot easier for you.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you use getline, it reads the entire line. So the entire print "Hello, World" text will be in the linha variable. You'll have to use string functions to remove the "print" and the double quotes.

    If all the commands are one word (no spaces) then you could just read in the first word of each line with operator >>. Then if it is "print" you could use getline to read the rest of the line, although you'd still have to get rid of the quotes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM

Tags for this Thread