new itoa function ?

itoa function is not defined in cygwin so i need to define one manually listed below"found in net" but I have an error saying
'can not convert std:basic-string<char>,std::char-traits<char>,std::allocator<char>>' to char* in intilization'
, basically I want to read multi files in one function readfrom_policyfile which takes integer and return string or char and put it in in the filename like file0.txt, file1.txt


readfrom_policyfile(int id)
{
std::string buffer;
buffer=itoa(id,10);

string *filename = "file"+id+".txt";
///
...}


std::string itoa(int value, int base) {

std::string buf;

// check that the base if valid
if (base < 2 || base > 16) return buf;

enum { kMaxDigits = 35 };
buf.reserve( kMaxDigits ); // Pre-allocate enough space.

int quotient = value;

// Translating number to string with base:
do {
buf += "0123456789abcdef"[ std::abs( quotient % base ) ];
quotient /= base;
} while ( quotient );

// Append the negative sign
if ( value < 0) buf += '-';

std::reverse( buf.begin(), buf.end() );
return buf;
}
Which line hives that error ? I don't see and conversions to a char* here..
What I do see is string *filename = "file"+id+".txt"; which does not do what you want it to..

Also, to convert numbers to strings, you can use a stringstream.
Last edited on
Hello Mate,

Sorry It should be, i forgot it , this line of code has the error
string *filename = "file"+buffer+".txt";
// reading the file



The Error in this expression "file"+buffer+".txt"
Well, that pointer makes no sense. What you want is the actual string object, created by adding the things on the right. Other than that, it compiled fine for me.
(Though the error is saying something different. If it doesn't go away, try changing "file" to string("file")..)
Last edited on
That is a known characteristic of the CygWin environment.
http://cs.nyu.edu/~yap/prog/cygwin/FAQs.html#itoa
Remember, itoa() is a non-standard function, so this can be expected...

If you are using C, skip itoa() altogether and use sprintf() instead.
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

 
#include <stdio.h> 
1
2
3
4
5
6
/*missing return type*/ readfrom_policyfile(int id)
{
  char filename[ 1024 ];
  sprintf( filename, "file%d.txt", id );
  ...
}

If you are using C++, skip itoa() altogether and use stringstreams instead.
http://www.cplusplus.com/reference/iostream/ostringstream/

1
2
#include <sstream>
#include <string> 
1
2
3
4
5
6
7
/*missing return type*/ readfrom_policyfile(int id)
{
  ostringstream oss;
  oss << "file" << id << ".txt";
  string filename = oss.str();
  ...
}

Hope this helps.
Thanks hamsterma and Duoas for the quick reply,
Hi Duoas,

YOU ARE BRILLIANT DUOAS MATE it works good with the 1 st solution.

but still wondering why it does not work with the 2nd sol, leads to an error in the line code: ifstream myfile (filename); as described below

void policy::readfrom_policyfile(int id)
{


ostringstream oss;
oss << "file" << id << ".txt";
string filename = oss.str();
//printf("filename has %s",filename);

ifstream myfile (filename);// THE ERROR IS HERE NOW SAYING NO MATCHING FUNCTION CALL TO STD::BASIC-IFSTREAM<CHAR-STD::CHAR-TRANSIT<CHAR>::BASIC -IFSTREM()STD::STRING&)


int line;
int group[2];
int i=0;
if (myfile.is_open()){ ....

But it works good with the first proposed solution YOU ARE BRILLIANT DUOAS THANKS

Last edited on
For the second solution, try ifstream myfile ( filename.c_str() );. The constructor of ifstream expects a const char*, not a string.
Topic archived. No new replies allowed.