-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileInputOutput.cpp
More file actions
75 lines (63 loc) · 1.98 KB
/
Copy pathFileInputOutput.cpp
File metadata and controls
75 lines (63 loc) · 1.98 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
#include<fstream>
using namespace std;
/*
The useful classes for working with files in C++ are:
1. fstreambase
2. ifstream --> derived from fstreambase
3. ofstream --> derived from fstreambase
*/
// In order to work with files in C++, you will have to open it. Primarily, there are two ways to open a file:
// 1. Using the constructor
// 2. Using the member function open() of the fstream class
/*
int main(){
// string st = "This is a text to write in a file";
string st2;
// ofstream ot("FileInputOutput.txt"); // Write operation
// ot << st<<endl; // Write the string to the file
// ot.close(); // Closing the file after writing operation
// Opening file using constructor and reading from it
ifstream ik("FileInputOutput.txt"); // Read operation
// ik >> st2; // Write the second string to the file --------> This show only the first word
getline(ik, st2); // This will get the line from the file
cout << st2 << endl; // Display the read string
getline(ik, st2); // This will get the next line from the file
cout << st2 << endl; // Display the read string
getline(ik, st2); // This will get the next line from the file
cout << st2 << endl; // Display the read string
return 0;
}
int main(){
ofstream out("FileInputOutput.txt");
cout << "Enter your name: ";
string name;
cin >> name;
out <<"My name is "+ name;
ifstream in("FileInputOutput.txt");
string content;
in >> content;
cout << "The file of this file is " << content;
return 0;
}
*/
int main(){
ofstream out;
out.open("FileInputOutput.txt");
out << "This is me\n";
out << "This is me alsoo\n";
out << "I'm an software engineer";
out.close();
ifstream in;
string st;
in.open("FileInputOutput.txt");
// in >> st;
// cout << st;
while (in.eof()==0) //eof -> end of file
{
getline(in, st);
cout << st << endl;
}
in.close();
return 0;
}