Thread: [Dev-C++] What lib should i be using instead?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Brian P. <BP...@go...> - 2001-07-27 22:32:30
|
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 |
|
From: jansb000 <jan...@wx...> - 2001-07-28 16:31:48
|
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
|
|
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...
|
|
From: jansb000 <jan...@wx...> - 2001-07-29 11:22:28
|
ITOA is a standard function. It resides in libc. Here is the description...
itoa
====
Syntax
------
#include <stdlib.h>
char * itoa(int value, char *string, int radix)
Description
-----------
This function converts its argument VALUE into a null-terminated
character string using RADIX as the base of the number system. The
resulting string with a length of upto 33 bytes (including the optional
sign and the terminating `NULL' is put into the buffer whose address is
given by STRING. For radixes other than 10, VALUE is treated as an
unsigned int (i.e., the sign bit is not interpreted as such). The
argument RADIX should specify the base, between 2 and 36, in which the
string reprsentation of VALUE is requested.
Return Value
------------
A pointer to STRING.
Portability
-----------
not ANSI, not POSIX
Example
-------
char binary_str[33];
(void)itoa(num, binary_str, 2);
----- Original Message -----
From: "Luigi Santillo" <gi...@li...>
To: <dev...@li...>
Sent: Saturday, July 28, 2001 8:15 PM
Subject: Re: [Dev-C++] What lib should i be using instead?
> 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...
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|