?
Solved

CString and cin >>

Posted on 1998-11-25
4
Medium Priority
?
1,162 Views
Last Modified: 2013-11-20
  I have a MFC console app that takes input from stdin via cin >> input; input is a char * and has been initialised in a constructor(input = "dummy";). Later in the app, I try to build a search string as shown below:
 
        CString str;
      CString pre = "'";
      CString post = "'";
      CString field = "PASSWORD = ";

//select record for this user_no

      pre += password;
      pre += post;
      str = field + pre;
      the_rec.m_strFilter = str;

The problem is that the CStrings are not receiving their expected values. e.g field does not show "PASSWORD = ", it is a null string. Others have various combinations of the input char *.
   When I comment out cin >> input; and simply assign a value to input, everything works OK. Is there some restriction on using stdin with CStrings or is there anything else which might cause this?
      
0
Comment
Question by:mspx
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1325328
CString has a constructor for const char * (LPCSTR) not for char *, that's why

if you want >> to work for CString, overload >>

istream& operator>> ( istream& is, CString& str )
{
   // <-- insert code for unpack whatever input into a CString
   return is;
}

A good way to get a char * buffer to the CString data is to use LockString (member of CString) giving you an LPSTR (char *), then UnlockString when you're done

Finally when you see LPCTSTR that's LPCSTR in 8 bit char set (ANSI) - i.e. const char * - it takes into acccount wide characters when compiling for UNICODE.  Similarly LPTSTR and LPSTR and char *.

0
 

Author Comment

by:mspx
ID: 1325329
  I'm not sure if I have made the problem clear. cin >> input is inserting data into a char *, not a CString. I have used a char * so that I can use the C string functions which meet my needs better than the CString string functions in this case. As I said, when i comment out cin >> input and use input = "aaaaaaetc.", the CStrings later in the program are assigned correctly. It appears as though merely using cin >> input somehow affects the way unrelated objects are assigned(CStrings in this case).
The only use for input(a char *) in the context of the CStrings concerned is that it is used to assign a value to password(also a char *).

The overloaded operator function you propose takes a CString & reference as one argument. As I said, the argument needs to be a char * so I can use C functions.

Am I missing the point?
0
 

Author Comment

by:mspx
ID: 1325330
see comment above
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 300 total points
ID: 1325331
if you have the following code:

  char* input = "hello";
  cin >> input;

this is invalid!

Instead you will need to do the following:

  char[MAX_PATH] input;
  strcpy(input, "Hello");

  cin >> input;

The reason is that in the first case, input has been initialized to a pointer to some area of memory that should not be overwritten. However, when the code cin >> input; is encountered, the library is trying to put the characters entered into that space which will definitely write over something else in that area. This causes your program to behavior erratically because its behavior is undefined.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question