char and char* problem

Posted on 2005-05-06
Last Modified: 2010-04-01
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?
Question by:tamirvs
    LVL 55

    Expert Comment

    by:Jaime Olivares
    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:
    sprintf("%i", b);

    and your are ready to call function:
    LVL 55

    Expert Comment

    by:Jaime Olivares
    sorry, must be:
    sprintf(bufint, "%i", b);
    LVL 30

    Expert Comment

    You can also use itoa if your compiler supports it.
    LVL 30

    Accepted Solution

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

    template<class T>
    string ToString(const T& source)
       ostringstream oss;
       oss << source;
       return oss.str();
    LVL 30

    Expert Comment

    The above ToString function will convert any object with <<() operator to a string.

    Author Comment

    thank you both, but Axter's code is more suitable for me. sorry jaime.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    maze travler 6 37
    Rijndael string encryption in C++ 3 88
    Visual c++ and text files 7 48
    How to gracefully close the c++ 11 thread? 3 47
    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now