C++/class
[C++] 외부 파일에서 데이터 읽어오기(fstream, infile)
year.number
2022. 6. 14. 21:02
#include <fstream> // open(), close()
#include <iostream>
using namespace std;
int main(){
ifstream infile; // 입력파일스트림
infile.open("data1.txt");
// ifstream infile("data1.txt"); // 선언과 open 한줄로
// 파일에서 12개의 데이터를 읽어서 배열(inumbers[4][3])에 저장
if (infile.is_open())
{
for (int row = 0; row < ROW; row++)
for (int col = 0; col < COL; col++)
infile >> inumbers[row][col];
}
else
{
cout << "파일을 열수 없습니다." << endl;
exit(0);
}
// 닫기
infile.close();
}
Key Point
1.
#include <fstream>
2.
ifstream infile; // 입력 파일 스트림
infile.open("data1.txt");
3.
for (int row = 0; row < ROW; row++)
for (int col = 0; col < COL; col++)
infile >> inumbers[row][col];