1 Filestream
C ์คํ์ผ ํ์ผ ์ ์ถ๋ ฅ
File Stream์ ๋ง๋ค์ด์ FILE ํ์ ์ ๋ณ์์ ๊ฐ์ง๊ณ ์๊ฒ ํด์ค๋ค. File Stream์ ํ์ผ์ ๋ง๋ค๊ฑฐ๋ ์ฝ์ด์ฌ ๋ ํด๋น ํ์ผ๊ณผ ์ฐ๊ฒฐ๋ ๋ณ์์ด๋ค.
์ ๋๊ฒฝ๋ก: ๋ณดํต ํ์๊ธฐ์ ํด๋ ๊ฒฝ๋ก. ๋๋ผ์ด๋ธ๋ถํฐ์ ์ ์ฒด ๊ฒฝ๋ก. ์๋๊ฒฝ๋ก: ํ์ฌ ํด๋๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ชฉ์ ์ง ๊ฒฝ๋ก๊น์ง์ ์๋์ ์ธ ๊ฒฝ๋ก.
๋ชจ๋ ๋ฌธ์์ด | ์๋ฏธ | ํ์ผ์ด ์์ ๋ | ํ์ผ์ด ์์ ๋ | ์ฝ๊ธฐ | ์ฐ๊ธฐ | ์ถ๊ฐ |
---|---|---|---|---|---|---|
r | ์ฝ๊ธฐ | ์ค๋ฅ ๋ฐ์ | ํ์ผ์ด๊ธฐ | O | X | X |
w | ์ฐ๊ธฐ | ํ์ผ ์์ฑ | ๋ฎ์ด์ฐ๊ธฐ | X | O | X |
a | ์ถ๊ฐ | ํ์ผ ์์ฑ | ๋ด์ฉ ์ถ๊ฐ | X | O | O |
r+ | ์ฝ๊ธฐ/์ฐ๊ธฐ | ์ค๋ฅ๋ฐ์ | ํ์ผ ์ด๊ธฐ | O | O | X |
w+ | ์ฝ๊ธฐ/์ฐ๊ธฐ | ์๋ก ์์ฑ | ๋ฎ์ด์ฐ๊ธฐ | O | O | X |
a+ | ์ฝ๊ธฐ/์ถ๊ฐ | ์๋ก์์ฑ | ์ด๊ธฐ, ์ถ๊ฐ | O | O | O |
rt | ํ ์คํธ์ฝ๊ธฐ | ์ค๋ฅ๋ฐ์ | ์ด๊ธฐ | O | x | x |
fopen_s, fclose, fputs, fputc, fgets, fgetc
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
#include <iostream>
int main()
{
FILE* fileStream = nullptr;
// 1๋ฒ ์ธ์์ FileStream์ ๋ง๋ค์ด์ค๋ค.
// 2๋ฒ ์ธ์๋ ํ์ผ์ ๊ฒฝ๋ก๊ฐ ๋ค์ด๊ฐ๋๋ฐ, ๋ณดํต ์๋๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํ๋ฉฐ,
// ์๋๊ฒฝ๋ก์ ๊ฒฝ์ฐ ํ์ฌ ํ๋ก์ ํธ ํ์ผ์ด ์๋ ๊ฒฝ๋ก๋ฅผ ๊ธฐ์ค์ผ๋ก ์ง์ ํ๋ค.
// 3๋ฒ ์ธ์๋ ํ์ผ์ ๋ง๋ค๊ฑฐ๋ ์ฝ์ ๋, ์ด๋ค ํ์ผ์ ๋ง๋ค๊ฑฐ๋ ์ฝ์์ง ์ต์
์ ์ง์ ํ๋ ๊ธฐ๋ฅ์ด๋ค.
// ์ฒซ๋ฒ์งธ : r, w, a
// ๋๋ฒ์งธ : t (ํ
์คํธํ์ผ), b (๋ฐ์ด๋๋ฆฌํ์ผ), +
// ํ์ผ ์ฐ๊ธฐ
// ํ
์คํธํ์ผ ์์ฑ
// ํจ์์ ์ธ์๋ก ์ด์คํฌ์ธํฐ๋ฅผ ์ค์ ํ์ฌ,
// ํจ์ ๋ฐ๊นฅ์ ๋ณ์ ์ฃผ์๋ฅผ ๋ฐ์์์ ๊ทธ ๋ณ์์ ๋์ ํ ๋น ํ๋ ์๋ฆฌ์ ๊ฐ๋ค.
fopen_s(&fileStream, "Test.txt", "wt");
// ํ์ผ ์คํธ๋ฆผ์ด ์์ ๊ฒฝ์ฐ(nullptr์ด ์๋๋ผ๋ฉด), ํ์ผ์ ๋ง๋ค ์ ์๋ค๋ ์๋ฏธ์ด๋ค.
if (fileStream)
{
// fputs : ํ์ผ์ ํ
์คํธ๋ฅผ ์ ์ฅํด์ฃผ๋ ํจ์
fputs("0xFEFF", fileStream);
fputs("File Stream !", fileStream);
fclose(fileStream);
}
// ํ์ผ ์ฝ๊ธฐ
fopen_s(&fileStream, "Test.txt", "rt");
if (fileStream)
{
char firstText = fgetc(fileStream);
printf("firstText : %c", firstText);
char text[256] = {};
// ๊ฐํ๋ฌธ์๊ฐ ์๋ ๊ณณ ๊น์ง ์ฝ์ด์จ๋ค.
// ๋ง์ฝ ๊ฐํ๋ฌธ์ ์์ด ํ์ผ์ ๋์ ๋๋ฌํ๋ค๋ฉด, ๊ฑฐ๊ธฐ๊น์ง๋ง ์ฝ์ด์ค๊ฒ ๋๋ค.
fgets(text, 256, fileStream);
printf("text : %s", text);
fclose(fileStream);
}
return 0;
}
๋ฐ์ด๋๋ฆฌ ํ์ผ ์ฐ๊ณ ์ฝ๊ธฐ (๋ฐฐ์ด, ๊ตฌ์กฐ์ฒด)
fwrite(&number, sizeof(int), 1, fileStream);
1๋ฒ ์ธ์์ ์ฃผ์๋ก๋ถํฐ 2๋ฒ ์ธ์์ ํฌ๊ธฐ * 3๋ฒ ์ธ์์ ๊ฐฏ์ ๋งํผ์ ๋ฐ์ดํธ๋ฅผ
ํ์ผ์ ๋ณต์ฌํ๋ค.
fread(&number, sizeof(int), 1, fileStream);
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
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
enum class EPlayerJob : unsigned char
{
None,
Knight,
Archer,
Magicion
};
struct FPlayerInfo
{
char Name[32] = {};
EPlayerJob Job = EPlayerJob::Knight;
int Attack = 0;
int Defense = 0;
int Hp = 0;
int HpMax = 0;
int Mp = 0;
int MpMax = 0;
};
int main()
{
/*
๋ฐ์ด๋๋ฆฌํ์ผ: ํน์ ๋ฉ๋ชจ๋ฆฌ์ ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋๋ก ํ์ผ์ ์ฎ๊ฒจ์ ์ ์ฅํ ๋ ์ฌ์ฉํ๋ค.
*/
FILE* fileStream = nullptr;
fopen_s(&fileStream, "TestBinary.tbf", "wb");
if (fileStream)
{
int number = 100;
// 1๋ฒ ์ธ์์ ์ฃผ์๋ก๋ถํฐ 2๋ฒ ์ธ์์ ํฌ๊ธฐ * 3๋ฒ ์ธ์์ ๊ฐฏ์ ๋งํผ์ ๋ฐ์ดํธ๋ฅผ
// ํ์ผ์ ๋ณต์ฌํ๋ค.
fwrite(&number, sizeof(int), 1, fileStream);
// ๋ฐฐ์ด ์ ์ฅ
int array[10] = {};
for (int i = 0; i < 10; i++)
{
array[i] = i + 1;
}
fwrite(array, sizeof(int), 10, fileStream);
// ๊ตฌ์กฐ์ฒด ์ ์ฅ
FPlayerInfo player;
strcpy_s(player.Name, "susu");
player.Attack = 100;
player.Defense = 50;
player.Hp = 500;
player.HpMax = 500;
player.Mp = 100;
player.MpMax = 300;
fwrite(&player, sizeof(FPlayerInfo), 1, fileStream);
fclose(fileStream);
}
fopen_s(&fileStream, "TestBinary.tbf", "rb");
if (fileStream)
{
int number = 0;
fread(&number, sizeof(int), 1, fileStream);
printf("number : %d\n", number);
// ๊ตฌ์กฐ์ฒด ์ฝ์ด์ค๊ธฐ
FPlayerInfo player;
fread(&player, sizeof(FPlayerInfo), 1, fileStream);
// ๋ฐฐ์ด ์ฝ์ด์ค๊ธฐ
int array[10] = {};
fread(array, sizeof(int), 10, fileStream);
for (int i = 0; i < 10; i++)
{
printf("array [%d] : %d\n", i, array[i]);
}
fclose(fileStream);
}
return 0;
}
C++ ์คํ์ผ ํ์ผ ์ ์ถ๋ ฅ
ofstream(์ฐ๊ธฐ), ifstream(์ฝ๊ธฐ),
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
#include <iostream>
#include <fstream> // fstsream ์ ์ถ๊ฐํด์ผ ์ฌ์ฉํ ์ ์์.
int main()
{
// 2๋ฒ ์ธ์์ std::ios::binary ์ ๋ฃ์ด์ฃผ๋ฉด ๋ฐ์ด๋๋ฆฌ ํ์ผ์ด ๋๋ค.
std::ofstream writeFile("Test2.txt", std::ios::binary);
// ํ์ผ์ ๋ง๋ค ์ ์๋ ์ํ์ธ์ง ์ฒดํฌํ๋ค.
if (writeFile.is_open() == false)
return 0;
writeFile << "ํ
์คํธ ์ถ๊ฐ\n";
writeFile << "ํ
์คํธ ํ ์ค ๋ ์ถ๊ฐ\n";
writeFile.close();
// ์ฝ์ด์ค๊ธฐ
std::ifstream readFile("Test2.txt");
if (readFile.is_open() == false)
return 0;
char text[256] = {};
// ๊ฐํ๋ฌธ์ ์ ๊น์ง ํ ์ค์ ์ฝ์ด์ค๊ณ , ๊ฐํ๋ฌธ์๋ ๋ฏธํฌํจ.
readFile.getline(text, 256);
// ๋ฌธ์๋ง ์ฝ์ด์ค๊ณ , ์คํ์ด์ค ๋ฐ, ๊ฐํ๋ฌธ์ ๋ฑ์ ์ ๊น์ง๋ง ์ฝ์ด์จ๋ค.
//readFile >> text;
printf("text : %s", text); // ํ
์คํธ ์ถ๊ฐ ์ถ๋ ฅ
readFile.getline(text, 256);
printf("text : %s", text); // ํ
์คํธ ํ ์ค ๋ ์ถ๊ฐ ์ถ๋ ฅ
return 0;
}
๋ฐ์ด๋๋ฆฌ ํ์ผ์ ๊ตฌ์กฐ์ฒด, ๋ฐฐ์ด ์ฝ๊ณ ์ฐ๊ธฐ
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
#include <iostream>
#include <fstream>
int main()
{
std::ofstream writeFile("TestBinary2.tbf", std::ios::binary);
if (writeFile.is_open() == false)
return 0;
int number = 100;
// const char*๋ก ํ๋ณํ์ ํด์ค์ผ ์ธ ์ ์๋ค.
writeFile.write((const char*)&number, sizeof(int));
int array[10] = {};
for (int i = 0; i < 10; i++)
{
array[i] = i + 1;
}
writeFile.write((const char*)array, sizeof(int) * 10);
writeFile.close();
// ์ฝ์ด์ค๊ธฐ
std::ifstream readFile("TestBinary2.tbf", std::ios::binary);
if (readFile.is_open() == false)
return 0;
// ์ ๋๋ก ์ฝ์ด์ค๋์ง ๋ณด๋ ค๊ณ ์ด๊ธฐํ
number = 0;
for (int i = 0; i < 10; i++)
{
array[i] = 0;
}
readFile.read((char*)&number, sizeof(int));
readFile.read((char*)array, sizeof(int) * 10);
for (int i = 0; i < 10; i++)
{
printf("array[%d] : %d\n", i, array[i]);
}
readFile.close();
return 0;
}
๋๊ธ๋จ๊ธฐ๊ธฐ