new itoa function ?

Aug 2, 2011 at 1:02pm
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;
}
Aug 2, 2011 at 1:17pm
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 Aug 2, 2011 at 1:17pm
Aug 2, 2011 at 4:55pm
Hello Mate,

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



Aug 2, 2011 at 5:47pm
The Error in this expression "file"+buffer+".txt"
Aug 2, 2011 at 5:47pm
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 Aug 2, 2011 at 5:48pm
Aug 2, 2011 at 6:00pm
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.
Aug 4, 2011 at 3:22am
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 Aug 4, 2011 at 4:00am
Aug 4, 2011 at 4:23am
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.