C++ 15주차
2023. 12. 13. 12:30ㆍ💻 프로그래밍 언어/C++
콘솔/파일 입출력
입/출력 스트림 객체

실습 11-2: 형식 설정 멤버함수
#include <iostream>
using namespace std;
int main() {
cout << "디폴트\n";
cout.width(10);
cout << -50 << endl;
cout << "[ * fill ]\n";
cout.fill('*');
cout.width(10);
cout << -50 << endl;
cout.width(10);
cout << 100.25 << endl; // 소수점도 한자리 차지
cout.width(10);
cout << "HanSH" << endl;
cout.fill(' ');
cout.precision(6); //소수점을 제외한 전체 자리수
cout << 12.34567 << endl;
cout << fixed; //소수점 이하의 자리수만 다루게 함
cout.precision(3);
cout << 12.34567 << endl;
return 0;
}//한문제정도나옴
▶ cout.width( ): 다음에 출력할 애의 출력 폭을 결정.
▶ cout.fill(' ' ): 비어있는 칸을 ( ' ')안의 문자로 채운다.(문자열 x, 문자만 o)
▶ cout.precision( ): 소수점을 제외한 전체 자리수
▶ cout.fixed;
cout.precision( ): 소수점 이하의 자리수
다음 프로그램의 실행 결과는?
#include <iostream>
using namespace std;
int main()
{
int num = 100;
cout << "10진수: " << num << endl;
cout << "16진수: " << hex << num << endl;
cout << "8진수: " << oct << num << endl;
return 0;
}

실습 11 -3: setw()/setfill()
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << "abcdefg\n";
cout << 12345 << endl;
cout << 123.45 << endl;
cout << "10칸\n";
cout << setfill('*');
cout << setw(10) << "abcdefg" << endl;
cout << setw(10) << 12345 << endl;
cout << setw(10) << 123.45 << endl;
return 0;
}
▶ cout << setfill(' ') = cout.fill( )
▶ cout << setw( ) = cout.width( )
setw와 setfill은 #include <iomanip>가 필요
실습 11-5: 파일로 출력하기
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream hout("test1.txt"); // 출력파일 스트림 객체 hout 선언
if (!hout) {
cout << "출력할 파일을 열 수 없습니다.";
return 1;
}
hout << "Park\n";
hout << 10 << endl << 30 << endl;
hout.close(); //파일 종결
return 0;
}
파일의 개방/종결 형식

파일의 개방/종결 형식

출력을 파일에 하는 방법
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream xxx("test2.txt"); // 출력파일 스트림 객체 hout 선언
xxx << "Lee\n";
xxx << 3 << endl << 13 << endl;
xxx.close(); //파일 종결
return 0;
} //한문제씩나옴
▶ ofstream 객체명("파일명");
실습 11-6: 파일로부터 입력받기
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream hin("test.txt"); // 입력파일 스트림 객체 hin 선언
if (!hin) {
cout << "입력할 파일을 열 수 없습니다.";
return 1;
}
char str[50];
int i, j;
hin >> str >> i >> j;
cout << str << " " << i << " " << j << endl;
hin.close(); // 파일 종결
return 0;
}
▶ ifstream 객체명("불러올 파일명");
실습 11-7: 파일 입출력을 수행하는 프로그램
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream hout("test.txt");
if (!hout) {
cout << "출력할 파일을 열 수 없음.";
return 1;
}
hout << "Han S. H. \n";
hout.close();
ifstream hin("test.txt");
if (!hin) {
cout << "입력할 파일을 열 수 없음.";
return 1;
}
char str[50];
hin >> str;
cout << str << endl;
hin.close();
return 0;
}
▶ 파일을 만들때: ofstream
▶ 파일을 불러올때: ifstream
실습 11-8: "test.txt"에 저장된 내용을 읽어들여 공백을 "*"로 채워 화면에 출력
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char ch;
ifstream hin("test.txt");
if (!hin) {
cout << "입력할 화일을 열 수 없음";
return 1;
}
hin.unsetf(ios::skipws);//공백 무시x
while (!hin.eof()) {
hin >> ch;
if (ch == ' ') ch = '*';
cout << ch;
}
hin.close();
return 0;
}
참조자(reference)
참조자(reference) : 매우 중요
▶ C++에서만 가능
▶ A reference is an alternative name for an object (Bjarne Stroustrup).
▶ 참조자를 사용하려면 파일명이 .cpp이어야 함
▶ 변수의 별명
● int & rx = x;
● rx는 x를 참조하도록 초기화된 정수형 참조자
● 참조자(rx)에 변화를 주면 그 타켓(x)도 변함
참조자(reference) 예
#include <iostream>
using std::cout;
using std::endl;
int main(void)
{
int x = 10;
int& rx = x;
cout << x << " " << rx << endl;
rx = rx + 10;
cout << x << " " << rx << endl; //참조자(rx)에 변화를 주면 그 타켓(x)도 변함
x = x + 10;
cout << x << " " << rx << endl; //타켓(x)에 변화를 주면 그 참조자(rx)도 변함
return 0;
}
출처: c++ 프로그래밍 강의 자료