Python/2.7 simple coding(+ c++)

파일 입출력.

qkqhxla1 2016. 10. 4. 15:23

파일에서 내용을 한줄씩 가져오기.


만약 첫글자가 깨지거나 그런 현상이 있으면 txt를 저장할때 인코딩 방식을 바꿔볼것.

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


int main()
{
	ifstream fin;
	fin.open("a.txt");
	char buf[100];

	if(fin.is_open())
		while(!fin.eof())
		{
			fin.getline(buf,100);
			cout<<buf<<endl;
		}
}

파일에서 내용을 한줄씩 출력하기.


ofstream으로 인스턴스를 만들어줘야 한다.

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

int main()
{
	ofstream fout;
	fout.open("a1.txt");

	if(fout.is_open())
		fout<<"라라\n";
	fout<<"ㅎ.ㅎ";
	fout.close();
}