9 Replies - 5892 Views - Last Post: 06 March 2010 - 09:15 AM Rate Topic: -----

#1 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 05-February 06

Converting char array to string

Posted 05 March 2010 - 10:29 AM

Hi

What I am trying to do is fetch a filename in my code, but I'm having trouble getting my head around chars and strings. Here's what I have so far:

string fileno(string name){
	int arraySize = sizeof(name);
	int start = 0;
	int end = arraySize - 1;
	string number = "";

	for(int i = 0; i < arraySize; i++){
		if(name[i] == "/"){
			start = i;
		}else if(name[i] == "."){
			end = i;
		}
	}

	for(int i = start; i <= end; i++){
		number = number + name[i];
	}

	return(number);
}

int main(int argc, char *argv[])
{
	string filename = fileno(argv[1]);
	return 0;

}



The errors I'm getting complain that const char * can't be converted to int. What am I doing wrong?


On a secondary note, is there a reason why taking file input on the main function (the argc, argv stuff) would stop ofstream from working? If so, how can I get around it?

Is This A Good Question/Topic? 0
  • +

Replies To: Converting char array to string

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Converting char array to string

Posted 05 March 2010 - 10:34 AM

 if(name[i] == "/"){
                        start = i;
                }else if(name[i] == "."){
                        end = i;
                }


You do not need double quotes when dealing with char:
if(name[i] == '/')
{
    start = i;
} 
else if(name[i] == '.')
{
    end = i;
}


That should work.

This post has been edited by sarmanu: 05 March 2010 - 10:36 AM

Was This Post Helpful? 1
  • +
  • -

#3 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 112
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Converting char array to string

Posted 05 March 2010 - 10:35 AM

use single quotations ( '' ) in your if statements. "i" is not the same as 'i'
Was This Post Helpful? 1
  • +
  • -

#4 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 866
  • View blog
  • Posts: 3,985
  • Joined: 09-June 09

Re: Converting char array to string

Posted 05 March 2010 - 10:45 AM

you want to use a function class find_last_of() from std::string

look
string fileno(const string &name)
{ 
	size_t found;
	found = name.find_last_of("/\\");
	cout<<"FOLDER : "<<name.substr(0,found)<<endl
	    <<"FILE : "<<name.substr(found+1)<<endl;
	
	return name.substr(found+1); //return filename
} 


Was This Post Helpful? 1
  • +
  • -

#5 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 05-February 06

Re: Converting char array to string

Posted 05 March 2010 - 10:59 AM

I knew C++ was picky but damn, ' instead of ". What is the reasoning behind that?

Thanks ImaSexy, but I don't fully understand what you've put. I'm not very good at all with C++ and I was having enough trouble with my approach.


You guys have absolutely no idea why drag-n-drop file input would interrupt ofstream?

This post has been edited by theholygod: 05 March 2010 - 11:01 AM

Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Converting char array to string

Posted 05 March 2010 - 11:05 AM

Single quotes are encoding a SINGLE character, where double quotes encodes an ARRAY OF CHARACTERS (a string). Example:
char X = 'd'; // X is a char, holding one character
std::string str = "something"; // double quotes used, telling us that there is more than one character in str
std::string str2 = "x"; // a string containing one character, this differs from char data type. How to convert this to a char? Use char X = str2[0].


Was This Post Helpful? 1
  • +
  • -

#7 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 05-February 06

Re: Converting char array to string

Posted 05 March 2010 - 11:06 AM

Thanks very much
Was This Post Helpful? 0
  • +
  • -

#8 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 05-February 06

Re: Converting char array to string

Posted 05 March 2010 - 01:09 PM

Ok, I've done a little bit of reading and I think the reason my ofstream isn't working is because I am already using a stream to read the dragged file.

Can someone point me in the right direction to solve this?
Was This Post Helpful? 0
  • +
  • -

#9 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 05-February 06

Re: Converting char array to string

Posted 05 March 2010 - 02:18 PM

Found the problem, thanks anyway.
Was This Post Helpful? 0
  • +
  • -

#10 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Converting char array to string

Posted 06 March 2010 - 09:15 AM

int arraySize = sizeof(name);

shouldn't that be name.size() ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1