21
10/25/06 CS150 Introduction to Computer 1 Reading from and Writing to Files

1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

10/25/06 CS150 Introduction to Computer Science 1 1

Reading from and Writing to Files

10/25/06 CS150 Introduction to Computer Science 1 2

Files (3.12)

Data stored in variables is temporary

Files are used to permanently store large amounts of data

We will learn how to write programs that cano Create files

o Write to files

o Read from files

This is similar to how we read from the keyboard and wrote to the screen

10/25/06 CS150 Introduction to Computer Science 1 3

Steps to Using Files

There are five steps that must be taken in order to use files in C++

1. Include header files

2. Define a file stream object

3. Open the file

4. Use the file

5. Close the file

10/25/06 CS150 Introduction to Computer Science 1 4

1. Libraries

To access files you will need to includeo <iostream>

o <fstream>

10/25/06 CS150 Introduction to Computer Science 1 5

2. File Stream Objects

ifstream inputFile;

ofstream outputFile;

fstream inAndOut;

File stream objects are the ways that you refer to the files you are usingo Can specify which input/output file to use

o May input from more than one file

o May output to more than one file

10/25/06 CS150 Introduction to Computer Science 1 6

3. Opening Files

inputFile.open(“filename”)

Same syntax for both input and output files

Filename is a string literal

Example:

ifstream inputFile;

inputFile.open(“input.dat”);

10/25/06 CS150 Introduction to Computer Science 1 7

Check File Opened Correctly

Before we start using the file for reading or writing, we should make sure that it opened correctly

if(!inputInfo == true)

{

cout << “Error opening input file “;

exit(1);

}

10/25/06 CS150 Introduction to Computer Science 1 8

== true

These two statements are equivalento if(!inputInfo == true)

o if(!inputInfo)

Even if you don’t have == true in your loop, C++ will put it there by default

This applies to all conditional statements in repetition and selection structures

10/25/06 CS150 Introduction to Computer Science 1 9

4. Using File Streams

Use input file variable wherever you use cin

Examples:o inputFile >> num;

Output output file variable wherever you use cout

Examples:o outputFile << num;

10/25/06 CS150 Introduction to Computer Science 1 10

20.1 Example: Writing to a File

The following program asks the user to input numbers and writes these numbers to a file

10/25/06 CS150 Introduction to Computer Science 1 11

Example#include<fstream>#include<iostream>using namespace std;int main(){

ofstream outputFile; int num;outputInfo.open("out.dat");if (!outputInfo){ cout << "*** Error opening file" << endl;

exit (1);}cout << "Enter a number (9999 to quit): ";cin >> num;while (num != 9999){

outputFile << num << " "; cin >> num;

}return 0;

}

10/25/06 CS150 Introduction to Computer Science 1 12

20.2 Reading from a File

Write a program that will read in a sequence of numbers (double) from a file and calculate the sum. Assume that the last number is the trailer (-9999)

10/25/06 CS150 Introduction to Computer Science 1 13

20.3 Reading Until the EOF

It is possible to read from a file until the end is reached

while (inputFile >> num)

{

cout << num << " ";

sum += num;

}

10/25/06 CS150 Introduction to Computer Science 1 14

20.4 Reading Characters

Write a program that reads in some text from a file and outputs that text to the screen

The file contains:

Hello Everyone!

I'm a file that

contains some text.

10/25/06 CS150 Introduction to Computer Science 1 15

Solution

ifstream inputFile;

char letter;

inputFile.open("in.dat");

if (!inInfo)

{

cout << "*** Error opening file" << endl;

exit (1);

}

while (inputFile >> letter)

{

cout << letter;

}

cout << endl;

10/25/06 CS150 Introduction to Computer Science 1 16

The Output

HelloEveryone!I'mafilethatcontainssometext.

What’s happened?!

All spaces, tabs, and new lines have been ignored.

This is because >> only reads visible characters

How can we read all characters so that the output looks exactly like the input

10/25/06 CS150 Introduction to Computer Science 1 17

Solution

ifstream inputFile;

char letter;

inputFile.open("in.dat");

if (!inInfo)

{

cout << "*** Error opening file" << endl;

exit (1);

}

while (inputFile.get( letter ))

{

cout << letter;

}

cout << endl;

10/25/06 CS150 Introduction to Computer Science 1 18

20.5 Problem

Consider the data file below, where - indicate spaces:--12--33.4-d--12.3-2--5

What values would be assigned to the variables for each of the statements below where inputFile is the file variable?

int i,j;double x,y;char ch;

o inputFile >> i >> x >> y;o inputFile >> i >> j;o inputFile >> ch >> i;o inputFile >> x >> y >> ch >> x;

10/25/06 CS150 Introduction to Computer Science 1 19

Problem 0 ( yournumber % 3)

Read in the following file and print out the sum of ALL the numbers in the file and print out the sum of the numbers on each line, except the first number. The first number tells you how many more numbers there will be on that line.

Output:

Line 1: 10Line 2: 6Line 3: 20Total: 48

File Example:

4 3 4 1 23 2 3 15 9 0 9 1 1

10/25/06 CS150 Introduction to Computer Science 1 20

Problem 1 ( yournumber % 3)

Read in the following file and print out the character you read and whether or not the letter is a vowel. At the end, print out how many of each vowel you found.

Output:a: Vowelb: Not Vowelc: Not VowelA: Vowele: Vowelf: Not Voweli: Vowel

a: 2e: 1i: 1o: 0u: 0

File Example:

abcAefi

10/25/06 CS150 Introduction to Computer Science 1 21

Problem 2 ( yournumber % 3)

Prompt the user for their first and last name and 50 numbers. Write the first and last name to a file followed by the numbers, two numbers on each line in reverse order.

Output file:Bob Smith4 999 3...1 9

Input:Name? Bob SmithNumber 1: 9Number 2: 4Number 3: 3Number 4: 99...Number 49: 9Number 50: 1