1.

#include <iostream>
#include <fstream>
using namespace std;

int main() {
	const char* file = "c:\\app\\test.txt";
	ifstream fin(file);
	if (!fin) {
		cout << "파일 열기 실패" << endl;
		return 1;
	}

	int ch;
	while ((ch = fin.get()) != EOF) {
		cout.put(ch);
	}

	fin.close();
}

2.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
	ifstream fin;
	fin.open("c:\\windows\\system.ini");
	if (!fin) {
		cout << "파일 열기 실패";
		return 1;
	}
	int line = 1;
	string s;
	while (getline(fin, s)) {
		cout << line++ << " : " << s << endl;
	}
	fin.close();
}

3.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
	ifstream fin("c:\\windows\\system.ini");
	if (!fin) {
		cout << "파일 열기 실패" << endl;
		return 1;
	}

	int ch;
	while ((ch = fin.get()) != EOF) {
		if (ch >= 'a' && ch <= 'z') ch -= 32;
		cout.put(ch);
	}


	fin.close();
}

4.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
	ifstream fin("c:\\windows\\system.ini");
	if (!fin) {
		cout << "파일 열기 실패";
		return 1;
	}

	ofstream fout;
	fout.open("c:\\app\\system.txt");
	if (!fout) {
		cout << "파일 열기 실패";
		return 1;
	}


	int ch;
	while ((ch = fin.get()) != EOF) {
		if (ch >= 'a' && ch <= 'z') ch -= 32;
		fout.put(ch);
	}

	fin.close();
	fout.close();
}

5.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
	ifstream fin("test.cpp");
	
	int ch;
	bool found = false;
	while ((ch = fin.get()) != EOF) {
		if (ch == '/') {
			if (found == false) // 슬래쉬 한개 발견
				found = true;
			else { // 연속 두개 발견
				fin.ignore(100, '\n');
				cout.put('\n');
				found = false; 
			}
		}
		else {
			if (found == true) { // 슬래시가 한 개 별도 문자로 있는 경우
				cout << "/";
				found = false; 
			}
			cout.put(ch);
		}
	}

	fin.close();
}

13.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int fread(ifstream& fin, vector<string>& v) {
   string line;
   int count = 0;
   while (getline(fin, line)) {
      v.push_back(line);
      count++;
   }
   return count;
}

void echo(vector<string>& v) {
   for (int i = 0; i < v.size(); i++) {
      string word = v[i];
      cout << i + 1 << ": " << word << endl;
   }
}

void search(vector<string>& v, string word) {
   for (int i = 0; i < v.size(); i++) {
      string vWord = v[i];
      int index = vWord.find(word); // 인덱스 리턴 (못찾으면 -1 리턴)
      if (index !=  -1) // word로 시작하는 단어 출력일 경우 if (index == 0)
         cout << vWord << endl;

   }
}


int main() {
   const char* file = "words.txt";
   vector<string> v;

   ifstream fin(file);
   if (!fin) {
      cout << "파일 열기 실패" << endl;
      return 1;
   }

   int n = fread(fin, v);
   cout << "words.txt파일 읽기 완료.." << n << "개 단어 읽음" << endl;
   echo(v);

   while (true) {
      cout << "단어>> ";
      string word;
      cin >> word;
      if (word == "exit") break;
      search(v, word);
   }


   fin.close();

}

#include <fstream>
#include <iostream>
using namespace std;

int main() {
   ifstream fin("c:\\app\\parrot.jpg", ios::binary);
   if (!fin) {
      cout << "open error" << endl;
      return 0;
   }
   ofstream fout("c:\\app\\img.jpg", ios::binary);
   if (!fout) {
      cout << "open error2" << endl;
      return 1;
   }

   //int c;
   //while (c = fin.get() != EOF) {
   //   fout.put(c);
   //}

   char buf[1024];
   while (true) {
      fin.read(buf, sizeof(buf));
      int n = fin.gcount();
      fout.write(buf, n);
      if (n < 1024) break;
   }

   fin.close();
   fout.close();
}

#include <iostream>
#include <string>
using namespace std;

template <class T> class MyStack {
   int tos;
   T data[10];
public:
   MyStack();
   void push(T element);
   T pop();
};

template <class T>
MyStack<T>::MyStack() {
   tos = -1;
}

template <class T> 
void MyStack<T>::push(T element) {
   if (tos < 9) {
      cout << "Full!" << endl;
      return;
   }
   data[++tos] = element;
}

template <class T>
T MyStack<T>::pop() {
   if (tos == -1) {
      cout << "EMPTY!" << endl;
      return 0;
   }
   retrun data[tos--];
}
int main() {
   MyStack<int> iStack;
   iStack.push(3);
   iStack.push(4.5); // 주의

+ Recent posts