File System

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

File System

Why is use the file system in program?

• If your input data is lengthy and you are planning to execute


your program many times, it is not convenient to input your
data from the keyboard.

• If you want to make only minor changes to the input data each
time you execute the program.

• For example, if your income is the same every month and only
your expenses change, it is cumbersome to repeatedly type the
same number for each month.
Why is use the file system in program?

• Program can read the file during execution instead of


receiving the input from the keyboard.

• Just like a input file, an output file


– Can have any acceptable file name
– Must be linked with a file pointer/file object before it is
used
– Must be opened before it is used
– Should be closed after it is used
READING/WRITING data
from/to a file using <fstream>
Opening and closing a file
 fstream fobj;
 fobj.open(“filename.txt”, R/W access mode);
Read mode (ios::in)
Write mode (ios::out)
 fobj.close();

Reading data from the file


 fobj>>ch;

Writing data into the file


 fobj<<“Hello1”;
#include<iostream> fstream rfptr;
#include<fstream> rfptr.open("NEWFILE.txt“, ios::in);
using namespace std; if(!rfptr)
int main() cout<<"\nFile is not found";
{ else
fstream wfptr; {
wfptr.open("NEWFILE.txt",ios::out); cout<<"\nIn File!!\n";
wfptr<<"HELLO1$"<<endl; while(!rfptr.eof())
wfptr<<"HELLO2$"; {
wfptr.close(); char ch;
rfptr>> ch;
if(ch=='$’)
newfile.txt cout<<endl;
HELLO1$ else cout<<ch;
HELLO2$
} //end while
} //end else
Output on console rfptr.close();
HELLO1 return 0;
HELLO2 } // main
READING data form a file using <stdio>

Opening and closing a file


 FILE *fileptr;
 fileptr = fopen(filename, access_mode);
Read mode (“r”)
Write mode (“w”)
 fclose(fileptr);

Read file using fscanf( ) function


 fscanf(fileptr, format_String, argument_list);
Here’s a file example.
#include<iostream>
#include<stdio.h> file.txt
using namespace std; 36
int main( ) 123 456.78
{
double x;
int i,k;

FILE *inptr;

inptr=fopen("file.txt","r");
fscanf(inptr,"%d",&i); Output:
fscanf(inptr,"%d %1f",&k,&x); 36
123
fclose(inptr); 5.34643e-315

cout<<i<<endl<<k<<endl<<x;
return 0;
}
Exercise
Write a program to read your score from last semester form an
input file named mark.txt, which has one line of data consisting
of four subject marks (no characters); for example,
80 70 60 65
compute your average mark, and write all the input data and
average mark on the screen.
WRITING output to a file
Opening and closing a file
 FILE *fileptr;
 fileptr = fopen(filename, access_mode);
Read mode (“r”)
Write mode (“w”)
 fclose(fileptr);

Write file using fprintf( ) function


 fprintf(fileptr, format_String, argument_list);
Here’s a example of writing data to file.
#include<iostream>
#include<stdio.h>
using namespace std;
fileOut.txt
int main( )
Week=7
{
Year=2020
double income=123.45;
Income=123.450000
double expenses=987.65;
Expenses=987.650000
int week=7;
int year=2020;

FILE *myfile;

myfile=fopen("fileOut.txt","w");

fprintf(myfile,"Week=%d\nYear=%d\n",week,year);
fprintf(myfile,"Income=%lf\nExpenses=%lf\n",income,expenses);

fclose(myfile);

return 0;
}
Exercise
Write a program that writes the multiplication tables (from 1 to
16) in a text file (mul.txt). The format that will be printed at the
text file must look like this
1*1=1
1*2=2
...
16*11=176
16*12=192
Thank You!!

You might also like