NOTE : -
THIS CONTENT IS NOT ORIGINAL
32. Character arrays can be used like files
Generally speaking, it is possible to do on character arrays the same operations as on files. This is very useful to convert data or manage memory arrays.
Here is a program that writes inside a character array:
A program that reads from a character string:
Generally speaking, it is possible to do on character arrays the same operations as on files. This is very useful to convert data or manage memory arrays.
Here is a program that writes inside a character array:
using namespace std;
#include
#include
#include
#include
int main ()
{
char a[1024];
ostrstream b(a, 1024);
b.seekp(0); // Start from first char.
b << "2 + 2 = " << 2 + 2 << ends; // ( ends, not endl )
// ends is simply the
// null character '\0'
cout << a << endl;
double v = 2;
strcpy (a, "A sinus: ");
b.seekp(strlen (a));
b << "sin (" << v << ") = " << sin(v) << ends;
cout << a << endl;
return 0;
}
| Output |
| 2 + 2 = 4 A sinus: sin (2) = 0.909297 |
A program that reads from a character string:
using namespace std;
#include
#include
#include
int main ()
{
char a[1024];
istrstream b(a, 1024);
strcpy (a, "45.656");
double k, p;
b.seekg(0); // Start from first character.
b >> k;
k = k + 1;
cout << k << endl;
strcpy (a, "444.23 56.89");
b.seekg(0);
b >> k >> p;
cout << k << ", " << p + 1 << endl;
return 0;
}
| Output |
| 46.656 444.23, 57.89 |
BIBILOGRAPHY / REFERENCE : - http://www.4p8.com/eric.brasseur/cppcen.html

No comments:
Post a Comment