```[This page was removed from the UMich site but I found it cached at google.com
Now you're looking at a local NU CCS copy
Search for String Streams in the document below -- RP Futrelle]

Introduction to I/O Statements

October 25, 2000

Horstman Ch. 10

Output

Declare the file variable for the output

ofstream outputData;

where ofstream is the file variable for output and outputData is the object for
the file.

The same command for input files will open the output file.

outputData.open(“output.dat”);

Writing to a file is just like writing to the screen.  Instead of cout we now
use the object name of the file.

int x;

cout << x;

outputData << x;

Example
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

void fillVector(vector<double>& v,double dValue,int& i)
{
//function will move a number into the vector
v[i] = dValue;

}

int printVector(vector<double>& o)
{
//declare file variable for output
ofstream outputData;

double sum = 0, average = 0;

//open file to write to
outputData.open("output.txt");

//validate file has opened
if (outputData.fail())
{
//program could not open file
cout << "Unable to Open output File\n";

//end program
return 1;
}

//function will print vector contents to an output file
// and calculate the average
for (int i = 0; i < o.size(); i = i + 1)
{

sum = sum + o[i];

outputData << "Student "<< i+1 << " scored a " << o[i] << "\n";

}
average = sum/i;

outputData << "The average score is " << average << "\n";

//close up file

outputData.close();

return 0;
}

int main(void)
{
//declare variable for input file

ifstream inputData;

//declare vector to hold data
int iVectorLength = 10;
vector<double> a(iVectorLength);

//open input file

inputData.open("input2.txt");

//validate file has opened
if (inputData.fail())
{
//program could not open file
cout << "Unable to Open File\n";

//end program
return 1;
}

double dTestScore;
int iRecNum = 0;
while (!inputData.fail())
{
//read the first number from the file

inputData >> dTestScore;

// call function to fill vector with data

fillVector(a,dTestScore,iRecNum);

iRecNum = iRecNum + 1;
}

//call function to write results to the screen
int ret = printVector(a);

if (ret > 0)
{
cout << "Program failed to write to file\n";
}
//close up file

inputData.close();

return 0;
}

Single characters can be written with the put method.

char ch = ‘A’;

inputData.put(ch);

Input/Output

Declare the file variable for input/output

fstream fileData;

where fstream is the file variable for input/output and fileData is the object
for the file.

The same command for input files will open the input/output file.

fileData.open(“file.dat”);

String Streams

Other stream classes can be used to read characters from a different source or
to send them to a different destination.

The istringstream class reads characters from a string and the ostringstream
class writes characters to a string.

Using istringstream you can read numbers that are stored in a string.

String streams require the sstream library.

#include <sstream>

Example from Horstman:

string input = “January 23, 1955”;
istringstream instr(input);

Now, use the >> to break the string into pieces

string month, comma;
int day, year;

instr >> month >> day >> comma >> year;

Conversely, by writing to a string stream, you can convert numbers to strings.

Construct an ostringstream object:

ostringsteam outstr;

Now, use the << operator to add a number to the string stream

outstr << 5.3;

To obtain the string from the stream call the member function str().

outstr.str();

Consider the creation of a string summarizing average score for the class on
exam number one.

string text1 = “The class scored an average of “;
double average = 85.9;
int examNumber = 1;
string text2 = “on Exam number “;

outstr << text1 << average << text2 << examNumber;

string sOutput = outstr.str();

String sOutput is now equal to the string “

The class scored an average of 85.9 on Exam number 1”

Command Line Arguments

When you invoke your programs from the command line (which you have been doing
for weeks), you can also type in additional information for use by your
program.

These are commonly referred to as command line arguments.

For example instead of starting the program at the prompt by calling just the
name

prompt% example.exe

prompt% example.exe text.txt

where text.txt is an input file name.

This technique is very useful in eliminating “hard codes” of file names and
other “volatile” variables.

For your program to understand command line variables the main function must be
arguments:

int argc

where arc is the count of arguments

char* argv[]

where argv denotes an array of pointers to C character arrays

int main (int argc, char* argv[])
{
…
}

Note in our example

argv[0]   is  example.exe

argv[1]   is text.txt

Another Example …

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc,char* argv[])
{
//declare variable for input file

string sFileName;
if (argc ==2)
{
sFileName = string(argv[1]);
cout << "The file entered is: " << sFileName << "\n";

}
else
{
cout << "Please enter file name after program name \n";
return 1;
}

ifstream inputData;

//open input file
inputData.open(argv[1]);

//validate file has opened
if (inputData.fail())
{
//program could not open file
cout << "Unable to Open File\n";

//end program
return 1;
}

int iTestScore;

//read the first number from the file

inputData >> iTestScore;

//write the results to the screen
cout << "The Student Scored a " << iTestScore << "\n";

return 0;
}

```