GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jan-2005, 20:08
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road

C++ string from file


hi everyone!
i have a code that reads from a text file and according to the
CPP / C++ / C Code:
char komut[10]
it does some specific operations.
CPP / C++ / C Code:
//ifstream constrctır opens the file
	ifstream inClientFile ( "tester1.txt", ios::in );

	//exitprogram if ifstream could not open file
	if(!inClientFile ) {
		cerr << "File could not be opened" <<endl;
		exit(1);
	} 


	int lineNo;
	char komut[10];
	char string[40];

	cout <<left<< setw(10) << "lineNo" << setw(13) << "komut" <<" string" << endl << fixed << showpoint;

	//display each line
	while(inClientFile >> lineNo >> komut >> string){
             switch(*komut) {
		case 'Print':
			cout << string <<endl ;
			break;
		case 'Input':
			cout << "?"<< endl;
			int x;
			cin >> x;
			break;
		case 'End':
			exit(1);
		}
	}
My problem is when my string have whitespaces it reads until there but i want all until the end of the line. and then after i take the string if my string is somthing like
CPP / C++ / C Code:
u=x+y
how could i seperate this string and use this variables with their own values??
thanks!!
  #2  
Old 04-Jan-2005, 20:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
To read the entire line you can use getline() instead. the ">>" operator stops at whitespace.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 04-Jan-2005, 20:39
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road
my text file is stg like this

00 Print give me 3 integers to operate
01 Input x
02 Input y
03 Input z
04 Let x=(x+y+z)/3
05 Print x
06 End

So actually i want to take them seperately as index, command and string but as the case like the first line i want to store all "give me 3 integers to operate" as string
what should i do for that case??
is it possible to take the first 2 with >> and then take them charachter by character till the end of the line?
  #4  
Old 05-Jan-2005, 08:29
dexter dexter is offline
Junior Member
 
Join Date: Jul 2004
Location: Queensland, Australia
Posts: 39
dexter will become famous soon enough
Based on the code you've got, I assume this is a basic interpreter.

Now, what you could do instead of,

CPP / C++ / C Code:
while(inClientFile >> lineNo >> komut >> string){

...is to have each case statement handle what is expected after the different types of komut... (especially if you intend to extend this interpreter further... you may have other tokens that don't require strings after them.)

So, instead:

CPP / C++ / C Code:
while(inClientFile >> lineNo >> komut){
    switch(*komut) {
    case 'Print':
      inClientFile.getline(string, 40, '\n');   // '\\n' not required as it is the default delimeter.
      cout << string <<endl ;
      break;

etc...

That said, your code in its current state will not compile... switch cases can't use strings to my knowledge... instead of using 'switch' I used 'if' and 'if else' statements with strcmp() tests when compiling the code.

Also, remeber to close your file at the end of the program...
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD Burner Help - Power Calibration Error.... JonBoy420 Computer Hardware Forum 110 05-Feb-2008 19:34
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:11.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.