-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile handling.cpp
68 lines (54 loc) · 1.54 KB
/
file handling.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
62
63
64
65
66
67
68
#include <iostream>
#include <fstream>
int main(){
//open mode
// std::ios::app -> append to file write
// std::ios::ate
// std::ios::binary -> binary read write;
// std::ios::out
//std::ios::trunc
//open("file",std::ios::binary | std::ios::app)
//read files
std::ifstream readFile ;
std::string content2 ;
readFile.open("array.cpp") ;
if (readFile.is_open()){
//check file is open
std::cout << "File Successfully Open..\n" ;
//read file content splted with space
while (readFile >> content2){
std::cout << content2 << '\n';
}
readFile.close() ;
}
readFile.open("array.cpp") ;
if (readFile.is_open()){
//check file is open
std::cout << "File Successfully Open..\n" ;
//read file content splted with line
while (std::getline(readFile ,content2)){
std::cout << content2 << '\n';
}
readFile.close() ;
}
//read as int
int number ;
readFile.open("nums.txt") ;
while(readFile >> number ){
std::cout << number << "\n" ;
}
//read character per
char content3 ;
readFile.open("array.cpp") ;
content3 = readFile.get() ;
std::cout << content3 ;
std::cout << (char)readFile.get() ;
//write files
std::ofstream writeFile ;
writeFile.open("Generate.txt") ;
//write 3 characters to file with end line
writeFile.write("123\n",4) ;
//write
writeFile << "Hello World\n" ;
writeFile << 1233123131313 ;
}