0% found this document useful (0 votes)
6 views

UNIT 03 - Lecture 26 - Working With Files

The document discusses various file input/output operations in C++ like put(), get(), write(), read() functions and how they handle sequential input/output of data. It also talks about reading and writing class objects and using command line arguments to pass file names. Functions like seek() are described for file pointer manipulations.

Uploaded by

Jelsteen J
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

UNIT 03 - Lecture 26 - Working With Files

The document discusses various file input/output operations in C++ like put(), get(), write(), read() functions and how they handle sequential input/output of data. It also talks about reading and writing class objects and using command line arguments to pass file names. Functions like seek() are described for file pointer manipulations.

Uploaded by

Jelsteen J
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Course: Object Oriented Programming using C++ (22CAU04A)

 Programme (s) : BCA

 Class : I B.C.A ‘A’

 Semester : II

 Unit & Lecture : 3 & 26

 Facilitator : Prof. J. JELSTEEN

SKASC 1
Topic to be Discussed

Working with Files


Sequential Input and Output Operations

 put( ) and get( ) Functions

The function put( ) writes a single character


to the associated stream.

The function get( ) reads a single charracter


from the associated stream.
//output
File will be created at
C:\TurboC++\Disk\TurboC3\BIN\ABC
Sequential Input and Output Operations
 write( ) and read( ) Functions
The functions write( ) and read ( ) handle the data
in binary form.
The values are stored in the disk file in the same
format in which they are stored in the internal
memory.
An int takes two bytes to store its value in the
binary form, irrespective of its size.
But a 4 digit int will take four bytes to store it in the
character form.
Sequential Input and Output Operations

Representing 2594
I 2 I
Bytes
0 0 0 0 1 0 1 0 0 1 0 0 0 1 Binary
0 0 Format
I 4 I
Bytes
2 5 9 4 Character
Format
Sequential Input and Output Operations

 infile.read((char *) &V, sizeof(V));

 outfile.write((char *) &V, sizeof(V));

 write( ) and read( ) functions take two


arguments.
 First is the address of the variable V
 Second is the length of that variable in bytes.
 The address of the variable must be cast to
type char * (pointer to character type).
Reading and Writing a Class Object

• The read( ) and write( ) are also used to read from


or write to the disk files objects directly.

• The read( ) and write( ) handle the entire structure


of an object as a single unit, using the computer’s
internal representation of data.

• Only data members are written to the disk files.


Command – Line Arguments
 C++ supports a feature that facilitates the supply of
argument to the main( ) function.

 These arguments are supplied at the time of invoking


the program.

 They are typically used to pass the names of data files.


 Eg:- exam data result

 The command-line arguments are typed by the user and


are delimited by a space.
Command – Line Arguments
 The main function can take two arguments.

 main( int argc, char * argv [ ] )

 The first argument argc (argument counter) represents


the number of arguments in the command line.

 The second argument argv (argument vector) is an


array of char type pointers that points to the command
line arguments.

 The size of the array will be equal to the value of argc.


Command – Line Arguments
 C:\> exam data results
 The value of argc would be 3 and argv would be an
array of three pointers to strings as:
 argv[0]  exam
 argv[1]  data
 argv[2]  results

○ ……
○ ……
○ infile.open(argv[1]);

……

……

outfile.open(argv[2]);

#include <iostream>

int main(int argc, char *argv[])


{
using namespace std;

cout << "There are " << argc << " arguments:" << endl;

// Loop through each argument and print its number and


value for (int nArg=0; nArg < argc; nArg++)
cout << nArg << " " << argv[nArg] << endl;

return 0;
}
#include <iostream>

int main(int argc, char *argv[])


{
using namespace std;

cout << "There are " << argc << " arguments:" << endl;

// Loop through each argument and print its number and


value for (int nArg=0; nArg < argc; nArg++)
cout << nArg << " " << argv[nArg] << endl;

return 0;
}
//output
Summary

 File Modes
 File Pointer
 Functions for Manipulations of File Pointers
• Seek Function with Absolute Position
• Seek Function with Specifying the Offset
 Sequential Input and Output Operations
 Reading and Writing a Class Object
 File operations
• ifstream
• ofstream
• fstream
• fopen()
• fclose()
• eof()
 Streams
• File input stream
• File output stream
Next Session
Function Templates

Thank You

You might also like