-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFile_ofstream_ifstream_create_write_read_0.cpp
61 lines (51 loc) · 2.02 KB
/
File_ofstream_ifstream_create_write_read_0.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// author: jaydattpatel
/* File Handling with C++ using fstream class object To write the Content in File Then to read the content of file
-----------------------------------------------------------------
fstream: It is used to create files, write information to files, and read information from files.
ifstream: It is used to read information from files.
ofstream: It is used to create files and write information to the files.
----------------------------------------------------------------
ios::in - (Open for input operations.)
ios::out - (Open for output operations.)
ios::binary - (Open in binary mode.)
ios::ate - (Set the initial position at the end of the file. If this flag is not set, the initial position is the beginning of the file.)
ios::app - (All output operations are performed at the end of the file, appending the content to the current content of the file.)
ios::trunc - (If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one.)
---------------------------------------------------------------
class default mode parameter
ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
*/
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
ofstream file_out_obj("sample.txt");
char str[]= "Hello world";
if(file_out_obj.is_open())
cout<<"File opened successfully to write\n";
else
{
cout<<"Error to Open File !!!";
exit(1);
}
file_out_obj.write(str,strlen(str)); //write string
file_out_obj.close();
//---------------------------------------------------------------
ifstream file_in_obj("sample.txt");
if(file_in_obj.is_open())
cout<<"File opened successfully to read\n";
else
{
cout<<"Error to Open File !!!";
exit(1);
}
char c[100];
file_in_obj.read(c,100); //to read string as per length
cout<<c;
file_in_obj.close();
return(0);
}