Connecting Tech Pros Worldwide Forums | Help | Site Map

read an entire line in a C++ string

Newbie
 
Join Date: Sep 2007
Posts: 2
#1: Sep 19 '07
hi ..
suppose there is a line at the command prompt with variable number of spaces . How can i scan it in a C++ string ??
a) cin cannot be used as it cannot scan spaces ...and scanf,gets cannot be used as it is a C++ string
If the line was in a file , i could have used
ifstream in("input.txt");
int main(){
string line
getline(in,line);
------------------
}
but if the line is at the command prompt then how do i scan it ??
debjani

sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#2: Sep 19 '07

re: read an entire line in a C++ string


Okay, I'm not entirely sure what you're asking here. If you are talking about a 'console' window, or the 'command line'.

The first one is the input/output window, the default cin/cout window. You can get the line by using

Expand|Select|Wrap|Line Numbers
  1. getline(cin, line);
  2.  
and specifying 'cin' as the source will allow you to pull that whole line.

However, if you are talking about the command line - where the program is instantiated, it's different. I'm using gcc, so my program binary is compiled as a.out, so as an example (so the ./a.out is me executing my program):

myPrompt# ./a.out paramater1 paramater2 paramater3

where you want paramaters[1-3], you have to "build them into a program" and have your main declaration look like this:

Expand|Select|Wrap|Line Numbers
  1. int main (int argc, char *argv[])
  2.  
Then you know how many parameters are there, and they are stored in the char* argv[] slots.

Is that clear, or have I confused you on something?
Newbie
 
Join Date: Sep 2007
Posts: 2
#3: Sep 19 '07

re: read an entire line in a C++ string


no ..
that is fine ...
i got it ...
thanks !!!!
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#4: Sep 19 '07

re: read an entire line in a C++ string


No problem, please post again if you get stuck.
Newbie
 
Join Date: Nov 2008
Location: India
Posts: 1
#5: Nov 21 '08

re: read an entire line in a C++ string


Quote:

Originally Posted by sicarie

No problem, please post again if you get stuck.

Here i am facing the same issue but i dont want to use gcc then how we can accompolish the task for windows command prompt ?
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,190
#6: Nov 21 '08

re: read an entire line in a C++ string


Everything in this thread is standard C++ and will work with any C++ compiler, you are not limited to gcc
Reply