Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- Linux
- 벤쿠버
- 오라클 10g
- Installation
- RHEL4
- 리눅스
- oracle
- vancouver
- Book
- 음식
- 맛집
- Trip
- 설치
- rac
- food
- 태그를 입력해 주세요.
- 내일로
- Oracle 10g
- 북미 동부 여행
- 노스 벤쿠버
- Toronto
- 설정
- canada
- North Vancouver
- 여행
- 구성
- 오라클
- cailisin
- 캐나다
- 토론토
Archives
- Today
- Total
...
ini 파일 생성 및 읽고 쓰기 본문
ini 파일 생성 및 읽고 쓰기
ini 파일이란 프로그램이 실행될 때 필요한 초기 정보를 담은 파일이다.
그러나 ini 파일의 구조에 맞춰 값을 저장 하기 위해서 사용하기도 한다.
Note
16비트 버전의 Windows 와 호환을 위해 제공되고 있으며 어플리케이션의 초기화 정보는 ini 파일 보다는 registry 를 권장하고 있다.
ini 파일의 구성은
- 섹션 (Section)
- 키 (Key)
- 문자열 값 (String Value)
이렇게 3개로 나누어지며 파일의 생성시 ini 파일의 내부는 다음과 같이 구성된다.
[section]
key=string
.
.
.
1. ini 파일 읽고 쓰기
파일 쓰기
ini 파일을 쓰거나 신규 생성 할 경우에는 WritePrivateProfileString
함수를 사용한다.
Syntax & Parameters
BOOL WritePrivateProfileString(
LPCSTR lpAppName, // 섹션 (Section) 이름
LPCSTR lpKeyName, // 키 (Key) 이름
LPCSTR lpString, // 값 (Value)
LPCSTR lpFileName // 저장할 ini 파일 경로
);
Remarks
- 위 함수를 사용시 ini 파일이 존재하지 않으면 신규 생성한다.
- 참고로 2번째 인자
lpKeyName
이 NULL 인 경우 해당 섹션 (Section) 이 삭제 되며 3번째 인자인lpString
이 NULL 인 경우 해당 섹션의 키 (Key) 를 삭제 할 수 있다.
// 섹션 (Section) 삭제
WritePrivateProfileString("Section_1", NULL, NULL, "C:\\test.ini");
// 키 (Key) 삭제
WritePrivateProfileString("Section_1", "Key_1", NULL, "C:\\test.ini");
파일 읽기
쓰여진 ini 파일에서 특정 내용을 읽어 올 경우에는 GetPrivateProfileString
함수를 사용한다.
Syntax & Parameters
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // 섹션 (Section) 이름
LPCTSTR lpKeyName, // 키 (Key) 이름
LPCTSTR lpDefault, // 해당 키 (Key) 값 없을 경우 리턴 할 기본값
LPTSTR lpReturnedString, // 값 (Value) 을 리턴 할 버퍼
DWORD nSize, // 버퍼 사이즈
LPCTSTR lpFileName // 읽어들일 ini 파일 경로
);
2. 예제
아래는 Section_1 의 Key_2 와 존재하지 않는 Section_3 의 Key_1 을 값을 찾아오는 간단한 예제이다.
Sample
// Multibyte Project Code
int main(void)
{
char *cBuf = NULL;
cBuf = (char *)malloc(sizeof(char) * 256);
memset(cBuf, 0x00, sizeof(cBuf));
// Write
WritePrivateProfileString("Section_1", "Key_1", "True", "C:\\test.ini");
WritePrivateProfileString("Section_1", "Key_2", "False", "C:\\test.ini");
WritePrivateProfileString("Section_2", "Key_1", "False", "C:\\test.ini");
WritePrivateProfileString("Section_2", "Key_2", "False", "C:\\test.ini");
// Read
GetPrivateProfileString("Section_1", "Key_2", "-", cBuf, 256, "C:\\test.ini");
printf("%s \n", cBuf);
GetPrivateProfileString("Section_3", "Key_1", "-", cBuf, 256, "C:\\test.ini");
printf("%s \n", cBuf);
return 0;
}
Result
위 Sample 코드를 실행하면 c: 에 test.ini 파일을 생성 후 다음과 같은 구성으로 Write 를 한다.
[Section_1]
Key_1=True
Key_2=False
[Section_2]
Key_1=False
Key_2=False
아래는 프로그램 실행 결과이다.
False
-
Section_1 의 Key_2 값을 찾아 달라고 하였기 때문에 'False' 가 출력되었고
두번째는 존재하지않는 Section 과 Key 를 입력하였기 때문에 설정한 기본값인 '-' 가 출력된 것이다.