Re: [Dev-C++] What lib should i be using instead?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Luigi S. <gi...@li...> - 2001-07-29 03:36:15
|
Hi Brian !
I'm not sure if the function "itoa" is ANSI C++; just in case it is not I
included a "small" version of it.
This code might do what you expect:
test.cpp
---
// main is just to let me test the code
#include <fstream.h>
#include <string>
#include <algorithm>
string
itoa(int n);
//////////////////////////////////////////////////////////////////////
int
main()
{
int number = 15;
string filename = "help";
ofstream data;
filename += itoa(number); // append the integer
data.open(filename.c_str()); // see STL help file for c_str()
data << "this string goes in the file: \"" << filename << "\"\n";
return 0;
}
//////////////////////////////////////////////////////////////////////
// convert a non-negative integer to a string
string
itoa(int n)
{
string s; // the number as a string
if (n >= 0) // convert
{
do
s += '0' + n % 10; // add in reverse order
while (n /= 10);
reverse(s.begin(), s.end()); // set in the right order
}
return s;
}
At 18.42 28/07/01 +0200, you wrote:
>I am not all to familiar with the string class that you are using, but
>"filename += number" may not do what you think.
>
>try this instead:
>
>//
>string filename = "help";
>int number = 0;
>filename += itoa(number); // convert int to char before appending it to
>string.
>cout << "{" << filename << "}" << endl;
>//
>
>You will now see that a space is added between filename and number. This
>character position is to represent a minus sign if number is negative. Your
>string class will probably have a function to get rid of this character.
>
>----- Original Message -----
>From: "Brian Puz" <BP...@go...>
>To: <Dev...@li...>
>Sent: Saturday, July 28, 2001 12:30 AM
>Subject: [Dev-C++] What lib should i be using instead?
>
>
> > I am using G++/string and fstream.h and am unable to open a file using a
>string. I want to dianamically create file names and would prefer to do this
>without char[].
> >
> > int number;
> > string filename = "help";
> > ofstream data;
> >
> > filename += number;
> > data.open(filename);
> >
> >
> >
> > ___________________________________________________
> > GO.com Mail
> > Get Your Free, Private E-mail at http://mail.go.com
> >
> >
> >
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
--
gisan
gi...@li...
|