GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jun-2005, 14:25
nasaiya nasaiya is offline
New Member
 
Join Date: Jun 2005
Posts: 16
nasaiya is on a distinguished road
Angry

convert char array into CString?


is it possible to convert a char array into a cstring?
i am trying to input strings from a file (using fstream) and i can't seem to use a cstring directly so i need to convert a char array to cstring. please help!
Last edited by LuciWiz : 20-Aug-2005 at 02:51. Reason: moved thread to the correct forum
  #2  
Old 04-Jun-2005, 14:37
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
What exactly do you mean? Do you want to be able to move from a string to a char *?

Example:
CPP / C++ / C Code:
#include <string>
#include <cstring>
using std::string;

string my_string;
char* my_char_ptr;

strcpy(my_char_ptr, my_string.c_str());

Or something of that nature? This is of course not actual code but I hope it exibits what you are after.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #3  
Old 04-Jun-2005, 14:46
nasaiya nasaiya is offline
New Member
 
Join Date: Jun 2005
Posts: 16
nasaiya is on a distinguished road
:edit: for somem reason when i try to define a string rather than a CString i get "undefined identifer" or something like that


this is basically what i'm triyng to do (this does not work)
CPP / C++ / C Code:
#include <fstream.h>

ifstream ccin;
CString cs;

ccin.open(data.dat);
ccin>>cs;
ccin.close();

the problem is that for some reason i can't use >> so i was going to use ccin.get or ccin.getline , but those require char arrays and i will have no way of knowing the size of the data being read into the variable

this is not a console app by the way if that makes a difference
Last edited by nasaiya : 04-Jun-2005 at 14:48. Reason: incomplete
  #4  
Old 04-Jun-2005, 16:14
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
can you use a regular std::string instead of a CString? Not using VC++, I don't know how to use CString, but with an std::string:
CPP / C++ / C Code:
#include <fstream>
#include <string>
using namespace std;
...
ifstream ccin("data.dat");
if (ccin.is_open())
{
string data = "";
getline(ccin, data);
}
else error();

also, @cable_guy_67:
std::string::c_str member function returns a const char pointer, so you can't write to it with strcpy.
  #5  
Old 04-Jun-2005, 16:28
nasaiya nasaiya is offline
New Member
 
Join Date: Jun 2005
Posts: 16
nasaiya is on a distinguished road
well i know that getline would work with a std::string but i still can't figure out why the compiler doesn't seem to recognize the declaration of a string.

i am using vc++ and the program is a dialog based mfc app. i tried putting the #include <string> in the app class's header file and the implementation cpp file but it still doesn't seem to include it or use it. i'm trying to do the file input within the App class's constructor... could this be why it doesn't work?
  #6  
Old 04-Jun-2005, 16:32
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
make sure to have somewhere
CPP / C++ / C Code:
using namespace std;
or just
CPP / C++ / C Code:
using std::string;
  #7  
Old 05-Jun-2005, 09:36
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Not to be picky or anything but ...

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <cstring>
using std::string;
using std::cout;
using std::endl;

int main(){
  string my_string = "my_string";
  char my_char_ptr[] = "my_char_ptr";
  // due to lack of length checking char* MUST be longer than string
  // this is just a simple example
  strcpy(my_char_ptr, my_string.c_str());
  cout << "strcpy(my_char_ptr, my_string.c_str()) = " << my_char_ptr << endl;

  return 0;
}
My output:
Code:
ME@mycpu~/WorkArea/FLTKforum/fltkPhoneBook $ g++ -Wall test_copy.cpp -s -o Test_Copy ME@mycpu~/WorkArea/FLTKforum/fltkPhoneBook $ Test_Copy strcpy(my_char_ptr, my_string.c_str()) = my_string ME@mycpu~/WorkArea/FLTKforum/fltkPhoneBook $ g++ --version g++ (GCC) 3.4.1 (cygming special) Copyright (C) 2004 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Go figure, I use it all the time. fltk dosen't use <string> so I use this when placing text in a display widget which is a char*. I may have been wrong in my assumption that by CString the OP meant a char array not a type named CString. So from <string.h>
Code:
char *strcpy(char *s1, const char *s2);

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #8  
Old 05-Jun-2005, 11:44
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
all right, fine. my mistake. i had the order of strcpy's parameters wrong
  #9  
Old 05-Jun-2005, 12:45
nasaiya nasaiya is offline
New Member
 
Join Date: Jun 2005
Posts: 16
nasaiya is on a distinguished road
would there happen to be a way to convert a std string to a CString or LPCTSTR? because the SetWindowText functions of some of my dialog box controls will only accept those or something in quotes but that won't work. i was able to define a std string after adding using std::string but i still can't pass the data from the string to the dialog box because of the CString/LPCTSTR requirement. any ideas?
  #10  
Old 05-Jun-2005, 15:56
nasaiya nasaiya is offline
New Member
 
Join Date: Jun 2005
Posts: 16
nasaiya is on a distinguished road
nevermind i got it-- thanks for the help
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ PhoneBook marita C++ Forum 46 12-Jun-2005 12:10
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
Compiling Errors ToddSAFM C++ Forum 22 18-Dec-2004 11:42
convert long to pointer to char realpopeye C++ Forum 2 26-Sep-2003 10:22

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.