//18
#include <iostream>
using namespace std;
int main() {
	cout << "디폴트\n";
	cout.width(10); //그 다음 cout 출력폭을 결정함
	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;
}
//22
#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;
}
//23

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
	cout << "abcdefg\n";
		cout << 12345 << endl;
	cout << 123.45 << endl;
	cout << "10칸\n";
		cout << setfill('*');//매개변수를 갖는 조절자 사용시#include <iomanip>필요,*로 채우기 문자를 설정한다
	cout << setw(10) << "abcdefg" << endl; //매개변수를 갖는 조절자 사용시#include <iomanip>필요
	cout << setw(10) << 12345 << endl;//매개변수를 갖는 조절자 사용시#include <iomanip>필요
	cout << setw(10) << 123.45 << endl;//매개변수를 갖는 조절자 사용시 #include <iomanip>필요,setwn으로 필드 폭을 설정한다
	return 0;
}

 

 

파일을 열고 다 가지고 놀면 닫고..? 파일을 개방하는 2가지 방법!

<출력을 파일에 하는 방법>

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	ofstream xxx("test2.txt"); // ofstream파일 만들기 출력파일 스트림 객체 hout 선언(hout말고 다른거 써도 됨)
	
	xxx << "kdhhhh\n";
	xxx << 12 << endl << 100 << endl;
	xxx.close(); //파일 종결
	return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{//abc.txt파일에 자신의 이름 저장하기
	ofstream xxx("abc.txt"); // 출력파일 스트림 객체 hout 선언
	
	xxx << "kdh\n";
	xxx.close(); //파일 종결
	return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{//만들어 놓았던 데이터를 불러올때 사용하는 방법(hin만는거)
	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;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	ofstream hout("test.txt");//파일입출력을 할때는ofstream으로 저장
	if (!hout) {
		cout << "출력할 파일을 열 수 없음.";
		return 1;
	}
	hout << "HanSH\n";
	hout.close();
	ifstream hin("test.txt");//파일을 불러올때는ifstream으로 불러옴
	if (!hin) {
		cout << "입력할 파일을 열 수 없음.";
		return 1;
	}
	char str[50];
	hin >> str;
	cout << str << endl;
	hin.close();
	return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{//출력ofstream 입력ifstream
	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;
}

#include <iostream>
using std::cout;
using std::endl;
int main(void)
{
	int x = 10;
	int& rx = x;//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;
}

출처: 1학년 2학기 수업자료/ 한성현 교수

 

'C++프로그래밍' 카테고리의 다른 글

C++프로그래밍 13주차 과제  (1) 2023.11.29
C++프로그래밍 12주차 과제  (1) 2023.11.22
C++프로그래밍 11주차 과제  (0) 2023.11.15
c++프로그래밍 10주차 과제  (1) 2023.11.08
C++ 프로그래밍 9주차 과제  (1) 2023.11.01

+ Recent posts