473,382 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

convert char * to string object

23
Hi please some one can help me. how to convert char * to string?

i have take char *argv[] from command line and want to pass to a function as string object(string str)
i want to first convert argv[1] to string object of type str, then pass to function().
please help me how to convert this
Nov 7 '08 #1
3 19813
arnaudk
424 256MB
A C string is a null-terminated character array. Thus, to convert your character array to a C string, simply append a null "\0" to the end of it. Note however that if your character array is in argv[1], then it will already be null-terminated because argv from main() is always an array of strings. If you want to use a C++ string, then simply declare one one passing the aforementioned C string to the constructor:
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. int main(int argc, char * argv[])
  3. {
  4. std::string str(argv[1]); // str: C++ string containing the characters of argv[1]
  5. ...
  6.  
Nov 7 '08 #2
mamul
23
A C string is a null-terminated character array. Thus, to convert your character array to a C string, simply append a null "\0" to the end of it. Note however that if your character array is in argv[1], then it will already be null-terminated because argv from main() is always an array of strings. If you want to use a C++ string, then simply declare one one passing the aforementioned C string to the constructor:
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. int main(int argc, char * argv[])
  3. {
  4. std::string str(argv[1]); // str: C++ string containing the characters of argv[1]
  5. ...
  6.  
Thanks A lot.
can u tell me why the value of result is 3 at the time of debuging.
please see the below code for renameing afile i want to use rename() of stdio.h
const char *oldname=argv[1];
const char *newname="input.docx";
int result=rename(oldname,newname);
cout<<"result "<<result;
rename() return -1 ? not renamed please tell me why? there is no compiler error, but its not renaming
Nov 7 '08 #3
arnaudk
424 256MB
Just to check things are working as you expect, why not print out oldname to the terminal? Also, are you sure the file exists in the current path? note that if you use back slashes "\" they will need to be escaped: "\\".
Nov 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Francesco | last post by:
Ciao, I'm new in c++ programming and I've experimented some method to convert real number into c++ string object, at present I've found as best solution use the C command "sprintf":...
3
by: Tim Mulholland | last post by:
How can i (using Managed C++) convert a System::String object containing basic text into a char* object? Thanks in advance, Tim
7
by: MilanB | last post by:
Hello How to convert char to int? Thanks
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
5
by: Alan Silver | last post by:
Hello, <dumb question> I am trying to do something similar to the following... string str = "Hello"; char c = (char)str.Substring(2,1); but the compiler complains that it cannot convert...
7
by: brian_harris | last post by:
I have a string object that I need to convert into an unmanged char * to be used by several unmnaged 3rd party functions. I have tried to use: (Marshal::StringToHGlobalAuto (Profname)) This would...
2
by: Joah Senegal | last post by:
Hello I need to convert a chat to a string... but I don't know how to do this. i;ve searched the internet but I've only find some code to convert char * to string or char to string.... but I...
3
by: simon | last post by:
I am a fresh Visual C++ .NET Developer. Can you kindly guide me for How to Convert char to System::String I am using windows forms and trying to set a text value referencing the method...
2
by: AndreasL | last post by:
Hi! I have a string passed to a methos via a string&. I would like to convert this to an unsigned long. How do I convert the string iterator to a char*? with all the error checking code...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.