nakka soft world !
C++ 파일 입출력 본문
C++ 파일 입출력
1. fstream 개념
#include <iosteam>
using namespave std;
class ostream {};
ostream cout;
class ofstrem : public ostream {};
int main()
{
cout << "hello";
}
2. 파일 출력
#include <fsteam>
int main()
{
cout << "hello";
ofstream fout("a.txt"); // 파일 생성 - 출력용
fout << "hello";
int n = 10;
fout << "n= " << n; // n = 10;
}
3. 파일 입력
#include <iosteam>
#include <fsteam>
#include <string>
int main()
{
ifstream fin("a.txt"); // 파일 입력
string s;
fin >> s; // 파일에서 한단어 입력
cout << s<< endl;
}
4. 파일 입출력과 STL예제
#include <iosteam>
#include <fsteam>
#include <string>
#include <vector>
#include <algorithm>
usnig namespace std;
int main()
{
ifstream fin("aaa.txt");
string s;
vector<string> v;
while( getline(fin, s) ){
cout << s << endl;
v.push_ack(s); // 파일로 부터 한줄씩 입력받아 vector에 입력함.
}
for(auto s1 : v)
cout << s1 << endl;
//--------------------
reverse(v.begin(), v.end()); // 앞뒤로 뒤집기
reverse(v[0].begin(), v[0].end()); // 1번줄([0])을 좌우로 뒤집기
for(int i=0; i<v.size(); i++) // 전체 좌우로 뒤집기
{
reverse(v[i].begin(), v[i].end());
}
for(int i=0; i<v.size(); i++) // 문자 변경 하기
{
replace(v[i].begin(), v[i].end(), 'i', 'x');
}
//---------------------
for(auto s1 : v)
cout << s1 << endl;
}
'프로그래밍언어 > C++' 카테고리의 다른 글
[C++] 간단한 Circular Queue (0) | 2019.05.08 |
---|---|
[MFC] CString <-> string (0) | 2017.04.27 |
iostream, cin (0) | 2017.03.27 |
예외 (Exception) (0) | 2017.03.27 |
다중 상속 (multiple inheritance), 다이아몬드 상속 (0) | 2017.03.27 |