C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-31-2009, 08:10 PM   #1
Trying to Learn C
 
nathanpc's Avatar
 
Join Date: Jul 2009
Location: Brazil
Posts: 32
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
__________________
Eee PC 904HD White | Windows XP Home Edition | My Site

Google Talk: eeepc904@gmail.com
ICQ: 424738586

Add me and let's talk about C and C++!
nathanpc is offline   Reply With Quote
Old 08-02-2009, 12:16 PM   #2
Unregistered User
 
Yarin's Avatar
 
Join Date: Jul 2007
Posts: 925
Read this, it'll make your task a lot easier for you.
__________________
GCC 4.4.0, Code::Blocks 8.02, Fedora 11, x64
Yarin is offline   Reply With Quote
Old 08-02-2009, 12:52 PM   #3
Registered User
 
Join Date: Jan 2005
Posts: 7,137
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.
Daved is offline   Reply With Quote
Reply

Tags
c++, cout, file-io

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Seg Fault in Compare Function tytelizgal C Programming 1 10-25-2008 03:06 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM
Unknown Memory Leak in Init() Function CodeHacker Windows Programming 3 07-09-2004 09:54 AM
finding strings in strings watshamacalit C Programming 14 01-11-2003 01:08 AM
Serial Communications in C ExDigit Windows Programming 7 01-09-2002 10:52 AM


All times are GMT -6. The time now is 08:34 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22