Home >  T.A >  cs10 ( Spring 2002 ) >  Lab 7

  CS10 Sec#24 : Topics covered in Lab #7
T.A: Demetris Zeinalipour
(Spring 2002)

Print this Page   
 [  Working w/ Files in C++  ::   fstream Standard C Library  ::   Exercise #7 ]



Files in C++ w/ Example

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

/* this Program reads a file with strings (string by string)
* and outputs the string on the screen
*/

void main()
{
    string filename;
    string line;

    while (true)
    {
        /* get the filename */
        cout << "Give me the name of the file: " ;
        cin >> filename;

        cout << "\nOpening \'" << filename << "\'...";

        /* open file for input */
        ifstream infilestream;
        infilestream.open(filename.c_str(), ios::in);

        if (infilestream)
        {
            cout << "done\n";
             // while the program hasn't reached the end of file read from the file and print on the screen
           
while (!infilestream.eof())

            {     infilestream >> line;
                  cout << line << endl;
             }
            break;
        }
        else
        {
            cout << "failed\n";
            cerr << "File could not be opened\n" << endl;             
        }

       // before quitting ALWAYS close any opened files
        infilestream.close();

    }
}

similarly you could open a file for outputting some data

        /* open another file for output */
        ofstream outputstream;
        outputstream.open("outputfile.txt",
ios::out);
       
outputstream << "I just wrote a sentence to the file!";
       
// before qutting ALWAYS close any opened files

        infilestream.close();

Top


C Standard Libraries
( Use fstream to add file connectivity to your programs )
fstream  ]
Top


Lab 7 - Exercise
New! - Download lab10.exe (win32) here and see how your program should correspond to various inputs.
Download numbers.txt (save it in same directory as the program)
See Solution - C++ solution here (will be available on Saturday since labs are running until friday)


Write a program that reads in integers from a file called "numbers.txt" and prints out the number and each individual digit of that number. You may assume that all the numbers in the file are positive values less than 10,000. The digits should line up nicely.
For example, if the file held the following values: 9627, 14, 396, 8, then your program would print something like the following:

You must read the values in from the file as integers, do not try and read in the digits.

Before running your program, you will need to create this file called numbers.txt. Follow the directions below:
  1. Start Microsoft Word
  2. Type in a bunch of integers (positive values less than 10,000), separated by whitespace.
  3. Go to "File", then "Save".
  4. For the "Save as Type", choose Text Only.
  5. For the "File Name", type numbers.
  6. Be sure to make it save in the folder in your account that contains your .cpp file.

Notice: Be sure to put all your information (login, lab section, …) at the top of your program. Your lab programs DO NOT get turned in electronically. I will come around and check you off individually.

Top