How to make strftime accept a Std::string?

Asked

Viewed 69 times

0

I put together a small example of how I tried to do it, but the string is empty, not receiving the value.

IMPORTANT I want to change only the strftime().

I tried to use, unsuccessfully

Data_Hora.c_str()  
Data_Hora.data.str().c_str()

Code:

#include <iostream>
#include <string>

int main() {
   time_t Capture = time(0);
   std::string Data_Hora;

   strftime((char*)Data_Hora.c_str(), 20, "%d/%m/%Y %T", localtime(&Capture)); // 25/05/2020 12:51:00

   std::cout << Data_Hora;
}

1 answer

1

You’re passing a reference to this function to fill in the data. You’re not passing the dice, because you don’t even have one, so it doesn’t make sense to convert one guy to another, you have to pass on what he expects, and if you want to use string then you move on to this.

How do I know this? Looking at the documentation. Ali shows that this first parameter is actually a form of data return and not a data passage. It turns out that the function returns the size of what it managed to do for you to validate. It is even a mistake to despise it, the correct would be to validate, I speak of it in We must disregard the return of functions in C that already receive the desired value by the parameter by reference?.

And the documentation clearly says for you to pass a vaiável of the type char *, can’t be a string. You cannot pass such a value. You are effectively passing one buffer, you have to pass a location to the function write to it.

You probably don’t understand What is a variable?. Since the variable is just a storage location and that’s what you want, you have to pass it pure. When you make a conversion and create an object of another type is not passing the variable, it is not what you want there. You are creating an object that is not stored anywhere, that is lost right after use (I will not go into detail would have to explain all memory allocation, show that you will get the return on an object that will be discarded instead of returning the value on an object that is stored and referenced).

There is no form of this function that accepts a string, then the conversion must be done after.

#include <iostream>
#include <string>
using namespace std;

int main() {
   time_t Capture = time(0);
   char text[20];
   strftime(text, 20, "%d/%m/%Y %T", localtime(&Capture)); // 25/05/2020 12:51:00
   string Data_Hora(text);
   cout << Data_Hora;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thanks for answering, but I don’t want to change the variable, the code I built is an example. I want to learn how to convert inside the strftime to Std::string for the format it uses in the char* case and the question I asked is to know whether or not it is possible to do this!

  • You have to change, the function works like this. There is a clear error in your code and I gave the solution. I used the code you posted. If you wanted to know in another context should put this context, I can only answer in the context you posed, including because the question does not say that you actually want to do other things. For another code the solution is clearly another, but this was not asked. And if the question is only whether or not it is possible to do something, the answer is yes. Has it helped anything? No. So this kind of question is closed, does not help anyone, I thought I wanted solution

  • In the strftime function I am passing rather a DICE. I am passing to it the time captured by the variable "Capture" that is formatted within the function, and this data is sent to the variable Data_hora also within the function.

  • But the question is about the string, does not matter other parties.

  • So you’re saying it’s impossible in this case to make this conversion would that be? That’s my question!

  • No, I did it in the code, the right way.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.