Solved

CString and cin >>

Posted on 1998-11-25
4
1,133 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
Comment Utility
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
Comment Utility
  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
Comment Utility
see comment above
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 100 total points
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Folder Comparison 12 52
wait notify demo infinite loop 3 80
Window placement 17 64
Modbus - whats the maximum I can store in one register? 4 74
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now