교재 문제

1번

.h

#ifndef _FILE_H
#define _FILE_H
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
namespace msy {
	
}
#endif

main.cpp

#include "file.h"
using namespace msy;
int main() {
	ifstream fin;
	fin.open("test.txt");
	if (!fin) { cout << "파일 오픈 실패"; return 0; }
	int s; string i;
	fin >> s;
	fin >> i;
	cout << s << endl;
	cout << i << endl;
	fin.close();
    return 0;
}

image.png

2번

.h

#ifndef _FILE_H
#define _FILE_H
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
namespace msy {
	
}
#endif

main.cpp

#include "file.h"
using namespace msy;
int main() {
	ifstream fin("c:\\windows\\system.ini");
	if (!fin) { cout << "파일오픈실패"; return -1; }
	string line;
	int i = 1;
	while (getline(fin, line))
	{
		cout << i <<" : "<< line << endl;
		i++;
	}
	fin.close();
    return 0;
}

image.png

3번

.h

#ifndef _FILE_H
#define _FILE_H
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
namespace msy {
	void upper(string& a);
}
#endif

main.cpp

#include "file.h"
using namespace msy;
int main() {
	ifstream fin("c:\\windows\\system.ini");
	if (!fin) { cout << "파일오픈실패"; return -1; }
	string line;
	int i = 1;
	while (getline(fin, line))
	{
		upper(line);
		cout << i << " : " << line << endl;
		i++;
	}
	fin.close();
    return 0;
}

.cpp

#include "file.h"

namespace msy {
	void upper(string& a) {
		for (int i = 0; i < a.size(); i++)
		{
			if (a[i] >= 'a' && a[i] <= 'z') {
				a[i] -= ('a' - 'A');
			}
		}
	}
}