This repository was archived by the owner on Jan 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileFunctions.cpp
More file actions
86 lines (78 loc) · 1.68 KB
/
FileFunctions.cpp
File metadata and controls
86 lines (78 loc) · 1.68 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
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
/// <summary>
/// Êîä èç ìåòîäè÷êè
/// </summary>
inline void test_read_file()
{
char fname[15], c;
size_t old_width = cin.width(15);
cout << "Enter input filename: ";
cin >> fname;
ifstream ifs;
ifs.open(fname);
if (!ifs)
{
cout << "Can't open input file" << endl;
ifs.close();
return;
}
cout << "Enter output filename: ";
cin >> fname;
ofstream ofs;
ofs.open(fname);
if (!ofs)
{
cout << "Can't open output file" << endl;
ifs.close();
ofs.close();
return;
}
while (ifs && ofs)
{
ifs.get(c);
c = toupper(c);
ofs.put(c);
cout << ".";
}
cout << endl << "Output file is a copy of input file but in upper case" << endl;
cin.width(old_width);
ifs.close();
ofs.close();
}
/// <summary>
/// Êîä èç ìåòîäè÷êè.
/// Çà îäíèì èñêëþ÷åíèåì - êîìïèëÿòîð vs èíà÷å îïðåäåëÿåò ôëàãè îòêðûòèÿ ïîòîêà(std::ofstream::_Noreplace èëè std::fstream::app), ÷åì ýòî îïèñàíî â òåêñòå.
/// </summary>
inline void test_write_file()
{
const char fname[] = "New file";
ofstream ofs;
ofs.open(fname, std::ofstream::out | std::ofstream::_Noreplace);
if (!ofs)
{
cout << "Can't open output file " << fname << ". Seems like it's already exists" << endl;
ofs.close();
return;
}
ofs << "First string in a new file";
ofs.close();
fstream fs;
fs.open(fname, std::fstream::out | std::fstream::app);
fs << "Addition to previous written string";
fs.close();
fstream ifs;
ifs.open(fname);
if (!ifs)
{
cout << "Can't open result file." << endl;
ifs.close();
return;
}
char line[80];
ifs.getline(line, sizeof(line));
cout << "Output file contains: " << endl << line << endl;
}