1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to convert cstrint to char*

Discussion in 'MFC' started by ankurk, Oct 9, 2007.

  1. ankurk

    ankurk New Member

    Joined:
    Aug 8, 2007
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    hi i want a to convert cstring variable contents into char *;


    how can i convert it, actually problem is that cstring variable is of textbox n i want to pass the contents to a function which takes parameters as char *.



    Thanks!!! :D
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Use the GetBuffer function but remember to use the ReleaseBuffer after you are done.
     
  3. ankurk

    ankurk New Member

    Joined:
    Aug 8, 2007
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    could you give me a snippet plz. i am working on windows ce actually
     
  4. ankurk

    ankurk New Member

    Joined:
    Aug 8, 2007
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    getbuffer does'nt work , i get only the first character of string not the whole string, could there be any problem due to character set in windows ce?
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    If you are using some compiler and an IDE you should be fine using the . to get the function.
     
  6. ankurk

    ankurk New Member

    Joined:
    Aug 8, 2007
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    i am using microsoft embedded c++ , compiler
    n no probs with .(dot).
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    So you should be able to use the functions I mention above. Why don't you give a try.
     
  8. ankurk

    ankurk New Member

    Joined:
    Aug 8, 2007
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    i tried but getbuffer() only gives me the first character of the string not the whole string
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
  10. ankurk

    ankurk New Member

    Joined:
    Aug 8, 2007
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    look i have this problem
    Code:
    int let;
     CString browse_file_name = " \\system\\texFile.TXT";
    
    const char * argument = browse_file_name.Getbuffer(browse_file_name.GetLength());
    
    FILE * file = fopen(argument,"r");
    	if(!file)
    	{
    		let = GetLastError();
    		MessageBox(_T("ERROR! file not found ..."));
    		exit(0);
    	}
    	else
    		MessageBox(_T("OK File found ..."));
    
    
    now the thing is that the variable argument does not get the whole path , just "\" only....
    please help

    i am using microsoft embedded vc++ compiler.
     
    Last edited by a moderator: Oct 11, 2007
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    My VC Compiler gives the correct output provided you use GetBuffer and not Getbuffer.
     
  12. ankurk

    ankurk New Member

    Joined:
    Aug 8, 2007
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    yes i am using the GetBuffer(); earlier that was a typing mistake,,


    still not getting the right output..., r u getting the whole string in the char * argument???
     
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
  14. ankurk

    ankurk New Member

    Joined:
    Aug 8, 2007
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    well god knows what's wrong then.......
    but i'll tell you it's not that simple i have found several queries similar to mine , but neither of the solutions provided there are useful.


    anyways thanks for your time
     
  15. ankurk

    ankurk New Member

    Joined:
    Aug 8, 2007
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    here is the correct solution use this version of fopen

    Code:
    int let;
    	UpdateData();
    
    	const wchar_t  * g_application_path  = (LPCTSTR) m_browse_file;
    
    	FILE * file = _tfopen(g_application_path ,_T("rb"));
    
    	if(!file)
    	{
    		let = GetLastError();
    		MessageBox(_T("ERROR! file not found ..."));
    		exit(0);
    	}
    	else
    		MessageBox(_T("OK File found ..."));
    
    
    just wanted to help if any one had similar problem
     
    Last edited by a moderator: Oct 13, 2007

Share This Page