Solved

CString and cin >>

Posted on 1998-11-25
4
1,147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 100 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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