I have a problem with strncpy. I need to change the first parameter to char for it to compile. I am not sure how I should do it. In C# I can just

int v1 = 123456;
string v2 = "";
 
v2 = v1.ToString();  // in C# this turns the number 123456 into the string "123456", (maybe itoa?)

Now I can use this v2 variable in a function (strncpy) that has string parameters since v2 is a string. How do I do this in C++?

private:

        int pollution_day[Pollution_day_MaxSize]; 
        int pollution_time[Pollution_time_MaxSize]; 
        int pollution_level[Pollution_time_MaxSize];
		

};

void Pollution::set_pollution_day(int * p_day, int p_day_sz)
{
        if ( p_day_sz >= Pollution_day_MaxSize) 
                p_day_sz = Pollution_day_MaxSize-1;

        int * sp_day = p_day;
        //trim end
        while( isspace(sp_day[p_day_sz-1])) p_day_sz--;
        //trim begining
        while( isspace(* sp_day))
        {
                sp_day ++;
                p_day_sz--;

        }
        strncpy (pollution_day, sp_day, p_day_sz); // getting an error here
        pollution_day[p_day_sz] = NULL;

		return;

};

I think what you are trying to do is convert a non-string to a string representation, yes?

If so, look into std::stringstream. You can simply do:

std::stringstream ss;
ss << yourThing;
std::string >> ss;

Dave

I think you may have that backward dave.

Shouldn't it be

std::stringstream ss;
std::string myString;
ss << yourThing;
ss >> myString;

???

Thank you!

So if I wanted to turn

int pollution_day

from int to char I would do this:

std::stringstream ss;
std::string pollution_day;
ss << pollution_day;

I'm sorry, I am confused..

Nope.

void NumberToString()
{
  int intNumber = 13;

  //put the number in a stringstream
  std::stringstream ss;
  ss << intNumber;

  //get the number out of the stringstream as a string
  std::string strNumber;
  ss >> strNumber;
  
  std::cout << "strNumber = " << strNumber << std::endl;
}

Dave

I think it's best you try and explain what you are trying to accomplish.
At the moment the use of strncpy isn't what it's intended for.

It seems to me you have a pollution_day array that you're trying to fill in the set_pollution function... but it's unclear what you want to fill it with.

In any case, you're kind of writing C code here (which can work of course but using C++ containers like vector may be much easier/more table ).

So if you give us some more details we may be able to help you come up with a better design.

Generally :

declare var v1
declare stringstream converter
declare var2 v2
converter << v1 //put variable 1 into stream, whether it be int,long,string...
converter >> v2 //convert var1, into int,long,float,string or whatever type var2 is.

I have to write a code that pulls three columns of 20 rows. Each row is a day 01, a time 00:00, and a level 00. I am trying to pull this data form the file, find the maximum level and what day and time it happened. I am getting an error on my strncpy. I have an int as first parameter and I need it to be char. I am trying to change the first parameter into char by changing it into a char string and using that string.

I don't see any reason to use strncpy? Why not just ifstream everything into a few std::vectors, use sort() from <algorithm> and then do any conversions to string as we just showed you?

This article has been dead for over six months. Start a new discussion instead.