• No results found

CSC 120 Lecture11 file

N/A
N/A
Protected

Academic year: 2020

Share "CSC 120 Lecture11 file"

Copied!
37
0
0

Loading.... (view fulltext now)

Full text

(1)

CSC-120

Programming

Fundamentals

(2)

Using Input/Output Files

 A computer file

is stored on a secondary storage device (e.g., disk);

is permanent;

can be used to

provide input data to a program

• or receive output data from a program

(3)

Streams

(4)
(5)

File Streams

(6)

General File I/O Steps

1. Include the header file fstream in the program.

2. Declare file stream variables.

3. Associate the file stream variables with the input/output sources.

4. Open the file

(7)

Header file

stream - a sequence of characters

interactive (iostream)

cin - input stream associated with keyboard.

cout - output stream associated with display

file (fstream)

ifstream - defines new input stream (normally associated with a file).

ofstream - defines new output stream (normally associated with a file).

(8)

File Streams

• ifstream (for reading from file) ofstream (for writing to file) fstream (read and write)

• Creating file streams

1. First define stream objects

ifstream [stream variable name]; ofstream [stream variable name]; fstream [stream variable name];

(9)

Declaring file stream

variables

ifstream

fsIn;

//input

ofstream

fsOut;

// output

(10)

Associate the file stream

variables with the I/O

sources

ifstream

fIn(“file1.txt”);

// input

ofstream

fOut(“file2.txt”);

// output

(11)

Opening File

Opening a file associates a file stream variable declared in the

program with a physical file at the source, such as a disk.

In the case of an input file:

the file must exist before the open statement executes.

If the file does not exist, the open statement fails and the input

stream enters the fail state

In the case of an output file:

An output file does not have to exist before it is opened;

if the output file does not exist, the computer prepares an

empty file for output.

If the designated output file already exists, by default, the old

(12)

More Input File-Related Functions

ifstream fsin;

fsIn.open(const char[] fname)

connects stream fsIn to the external file fname.

fsIn.get(char& character)

extracts next character from the input stream fsIn and

places it in the character variable character.

fsIn.eof()

(13)

More Output File-Related

Functions

ofstream fsOut;

fsOut.open(const char[] fname)

connects stream fsOut to the external file fname.

fsOut.put(char character)

inserts character character to the output stream fsOut.

fsOut.eof()

(14)
(15)
(16)

ifstream Operations

in >> stream extraction operator obj.get ( ) extract one character

(17)

Read char by char

(18)

Read line by line

(19)

Validate the file before

trying to access

First method Second method

By checking the stream

variable;

If ( ! both)

{

cout << “Cannot open file”;

}

By using

bool

is_open()

function.

If ( !both.is_open()) {

cout << “File is not open.”;

(20)

Stream

Manipulators

Stream manipulators go directly into the stream.

They are inserted using the stream insertion operator

(21)

File Open Mode

Name Description ios::in Open file to read

ios::out Open file to write

ios::app All the data you write, is put at the end of the file. It calls ios::out

ios::ate All the data you write, is put at the end of the file. It does not call ios::out

ios::trunc Deletes all previous content in the file. (empties the file)

ios::nocreate If the file does not exists, opening it with the open() function gets impossible.

ios::noreplace If FileName is a new file, then it gets created fine.If the file exists, trying to open it with the open() function, returns an error.

(22)

File Open Mode

#include <fstream> int main(void)

{

ofstream outFile("file1.txt", ios::out); outFile << "That's new!\n";

outFile.close(); Return 0;

}

(23)

Example writing to file

#include <iostream>

#include <fstream>

using namespace std; int main()

{

ofstream outFile;

// open an exist file fout.txt

outFile.open("number.txt",ios::app);

if (!outFile.is_open())

{ cout << " problem with opening the file ";}

else {

outFile <<200 <<endl ;

cout << "done writing" <<endl;}

(24)
(25)

Dealing with Binary files

Functions for binary file handling

get():

read a byte and point to the next byte to read

put():

write a byte and point to the next location for write

obj.read(char[],size):

block reading

obj.write(char[],size):

block writing

(26)
(27)

Dealing with Binary files

Some useful functions

obj.seekg():

Go to a specific position when reading

obj.seekp():

Go to a specific position when writing

tellg()

: Retunrs an int type, that shows the current position of the

inside-pointer. This one works only when you read a file.

tellp():

The same as tellg() but used when we write in a file.

(28)
(29)
(30)

String Streams

A stringstream reads/writes from/to a string rather

than a file or a keyboard/screen

(31)

String Streams

(32)

#include <iostream> #include <string> #include <sstream> using namespace std; int main ()

{

string str1;

stringstream ss; getline(cin,str1);

ss<< str1 << " number" ; int a;

ss>>a; a++;

cout<<ss.str() ;}

(33)

#include <iostream> #include <string> #include <sstream> #include<fstream> using namespace std; int main ()

{

stringstream ss;

ifstream infile(“file.txt”); string line;

int total=0;

(34)
(35)
(36)
(37)

References

Related documents

Neo Cortex Remastered (Cortex Strikes Back).png]]..

the group of fuzzy rules, average length of a fuzzy rule in the group, average length of the longest fuzzy rule and average length of the shortest fuzzy rule. On average, when

Properly authenticated, an FTP Client can access OCS file system functions, which include file read, file create, file delete, file rename, file write, directory read, directory

• Write values of primitive data types to a file and read them back from the file, using DataOutputStream

-f file file exists and is a regular file -r file You have read permission on file -s file file exists and is not empty -w file You have write permission on file file1 -nt

Elaine Farndale is Associate Professor in Human Resource Management at the School of Labor and Employment Relations at the Pennsylvania State University, and is also affiliated to

In terms of the main political economy models, the liberal market economies and social democratic economies have experienced on average rather more severe recessions than the

In the Third District, the number of borrowers rose from just over 1.1 million (11.5 percent of the CCP) at the start of 2005 to just under 1.8 million (17.5 percent of the CCP)