• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

char and char* problem

I'm reading a file into buf[n] and then i use one element of buf as an argument of the func:
voif foo(char* p);
but one element in buf is int and i can't cast it to char*, so what do i do?
0
tamirvs
Asked:
tamirvs
  • 3
  • 2
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
if you have many elements in buffer you can read the buffer as usual, then use sscanf to extract the integer, something like this:

int a,b,c;
sscanf(buf, "%i %i %i", &a,&b,&c)

then put desired integer in a secondary buffer, let's say:
bufint[10];
sprintf("%i", b);

and your are ready to call function:
foo(bufint);
0
 
Jaime OlivaresSoftware ArchitectCommented:
sorry, must be:
sprintf(bufint, "%i", b);
0
 
AxterCommented:
You can also use itoa if your compiler supports it.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AxterCommented:
The C++ way to do it, is to use stringstream.
Example:
#include <sstream>
#include <string>
using namespace std;

template<class T>
string ToString(const T& source)
{
   ostringstream oss;
   oss << source;
   return oss.str();
};
0
 
AxterCommented:
The above ToString function will convert any object with <<() operator to a string.
0
 
tamirvsAuthor Commented:
thank you both, but Axter's code is more suitable for me. sorry jaime.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now