I want to read a long number from file then increment it and write it back to file.
I am struggling with the convertion from string to long and back again.
I tried:
double id = atof("12345678901"); //using atof because numbers are too big for atio()
id++;
ostringstream strs;
strs << static_cast<long>((static_cast<double>(threadId)));
string output = strcpy_s(config->m_threadId, 20, strs.str().c_str());
But that converts the input to a negative or wrong number.
atoiis for normal integers. There’s alsoatolandatoll(_atoi64in windows):As suggested by one commenter, use
strtollinstead of theato*functions:Since you’re using C++ here, you should just pull it straight from the fstreams: