Streams and Files

Hello everyone, I was busy due to my college .
but now I return to c++ and to my old problem with streams and files chapter
this chapter is almost done but there still two mysteries topics
the first :Memory as a Stream Object
and this is it,s program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  // ostrstr.cpp
// writes formatted data into memory
#include <strstream>
#include <iostream>
#include <iomanip> //for setiosflags()
using namespace std;
const int SIZE = 80; //size of memory buffer
int main()
{
char ch = 'x'; //test data
int j = 77;
double d = 67890.12345;
char str1[] = "Kafka";
char str2[] = "Freud";
char membuff[SIZE]; //buffer in memory
ostrstream omem(membuff, SIZE); //create stream object
omem << "ch=" << ch << endl //insert formatted data
<< "j=" << j << endl //into object
<< setiosflags(ios::fixed) //format with decimal point
<< setprecision(2) //two digits to right of dec
<< "d=" << d << endl
<< "str1=" << str1 << endl
<< "str2=" << str2 << endl
<< ends; //end the buffer with '\0'
cout << membuff; //display the memory buffer
return 0;
}

the second : Command-Line Arguments
and this is it,s program
1
2
3
4
5
6
7
8
9
10
11
  // comline.cpp
// demonstrates command-line arguments
#include <iostream>
using namespace std;
int main(int argc, char* argv[] )
{
cout << "\nargc = " << argc << endl; //number of arguments
for(int j=0; j<argc; j++) //display arguments
cout << "Argument " << j << " = " << argv[j] << endl;
return 0;
}
Last edited on
First, the strstream is deprecated. Move on to use std::stringstream:
http://www.cplusplus.com/reference/sstream/stringstream/


A stream has interface; how it is used. The formatted input and output operators are part of the interface. You can perform same operations on every stream.

When you do read from a stream, the data does not come from void; the data exists somehere. A file stream reads from file, a stringstream reads from a string.


When a program is run, it might be given arguments. If you do run from command line, you type those arguments (command line options), but a GUI, like Windows Explorer may supply arguments too.

The command line is a list of words. There are argc words. The argv is the array of words. Each word is a C-string; an array of char that has null as last character.
Topic archived. No new replies allowed.