반응형
#include <io.h> int read(int handle, void *buf, unsigned len); |
read 함수는 hanle로 지정된 파일로부터 len 바이트의 문자를 읽어 buf에 저장합니다.
파일이 텍스트 모드로 열려있으면 캐리지 리턴을 제거하며, Ctrl_Z를 만나면 EOF는 반환합니다.
read 함수가 읽어 들일 수 있는 바이트의 최대 크기는 65,534 바이트입니다.
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>
int main()
{
int handle;
char s[100] = { 0 };
handle = open("open.txt", O_RDONLY);
if (handle == -1)
{
perror("Error: ");
return 1;
}
read(handle, s, 100);
printf("%s\n", s);
close(handle);
return 0;
}
반응형
비주얼스튜디오에서는 _open, _read, _close 함수를 사용합니다.
#define _CRT_SECURE_NO_WARNINGS // Visual Studio
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>
int main()
{
int handle;
char s[100] = { 0 };
handle = _open("open.txt", O_RDONLY); // O_CREAT | O_TEXT);
if (handle == -1)
{
perror("Error: ");
return 1;
}
_read(handle, s, 100);
printf("%s\n", s);
_close(handle);
return 0;
}
반응형
'C_C++ > C_라이브러리_함수' 카테고리의 다른 글
(C언어) write: 파일에 데이터를 쓴다 (0) | 2023.03.16 |
---|---|
(C언어) ultoa: 숫자를 원하는 진법의 문자열로 변환한다 (0) | 2023.03.14 |
(C언어) tolower, toupper: 문자를 소문자 또는 대문자로 변환한다 (0) | 2023.03.09 |
(C언어) system: DOS 명령어를 실행합니다. (0) | 2023.03.09 |
(C언어) strset: 문자열을 지정된 문자로 모두 바꾼다 (0) | 2023.03.08 |