Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- w3school
- github
- 파이썬
- 프로그래밍
- UE5
- c++
- Unity
- python
- 문제풀이
- Programming
- Tutorial
- 재귀
- 시작해요 언리얼 2022
- loop
- 백준
- String
- Algorithm
- Unreal Engine 5
- Basic
- Material
- 기초
- W3Schools
- C#
- dfs
- 오류
- guide
- dynamic
- Class
- parameter
- DP
Archives
- Today
- Total
행복한 개구리
C++ Classes - Files 본문
C++ 파일
fstream 라이브러리는 우리가 파일로 작업할 수 있게 해줍니다.
fstream 라이브러리를 사용하려면 표준 <iostream> 그리고 <fstream> 헤더 파일을 둘 다 포함해야합니다:
예시
#include <iostream>
#include <fstream>
fstream 라이브러리는 우리가 파일을 생성하거나, 작성하거나, 읽게해주는 세 개의 클래스를 포함합니다.
파일 생성하고 작성하기
파일을 생성하기 위해서는 ofstream 또는 fstream 클래스를 사용하고 파일의 이름을 특정하면 됩니다.
파일에 작성하려면 삽입 연산자 << 를 이용하세요.
예시
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// 텍스트 파일을 생성하고 열기
ofstream MyFile("filename.txt");
// 파일에 작성하기
MyFile << "Files can be tricky, but it is fun enough!";
// 파일 닫기
MyFile.close();
return 0;
}
파일 읽어오기
파일을 읽어오기 위해서는 ifstream 또는 fstream 클래스를 사용하고 파일 이름을 알려주면 됩니다.
우리는 또한 파일의 내용을 줄마다 읽어와서 출력하기 위해서 while 반복문을 getline() 함수( ifstream 에 귀속되는 함수)와 사용하고 있는 것을 참고하세요.
예시
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// 텍스트 파일을 출력할 때 쓰일 텍스트 문자열 만들기
string myText;
// 텍스트 파일 읽어오기
ifstream MyReadFile("filename.txt");
// 파일을 줄마다 읽어오기 위해 while 반복문과 getline() 함수를 함께 사용하기
while (getline(MyReadFile, myText)) {
cout << myText;
}
return 0;
}
* getline()함수를 위해서 <string> 라이브러리를 포함시키세요.
'C++ > 공부내용' 카테고리의 다른 글
C++ Classes - Exceptions (0) | 2022.06.30 |
---|---|
C++ Classes - Polymorphism (0) | 2022.06.30 |
C++ Classes - Inheritance / Access Specifiers (0) | 2022.06.29 |
C++ Classes - Inheritance / Multiple Inheritance (0) | 2022.06.29 |
C++ Classes - Inheritance / Multilevel Inheritance (0) | 2022.06.29 |