![]() |
| | #1 |
| Trying to Learn C Join Date: Jul 2009 Location: Brazil
Posts: 32
| 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;
}
Code: print "Hello, World" 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 | |
| | #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 | |
![]() |
| Tags |
| c++, cout, file-io |
| Thread Tools | |
| Display Modes | |
|
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 |