(언어 C) 쓰기: 데이터를 파일에 씁니다.

#포함하다
int write(int 핸들, void *buf, unsigned len);

쓰기 기능은 핸들이 가리키는 파일에 길이 len에 대한 buf의 내용을 저장합니다.

한 번에 쓸 수 있는 바이트 수는 65,535바이트이며 실제로 쓴 바이트 수가 지정된 수보다 적으면 오류로 간주합니다. 하드 드라이브 오류로 인해 이 오류가 발생할 수 있습니다.

반환 값

쓴 바이트 수를 반환합니다.

오류가 발생하면 -1이 반환되고 전역 변수 errno가 다음 값 중 하나로 설정됩니다.

  • EACCES: 작업이 거부되었습니다.
  • EBADF: 파일 번호(핸들)가 올바르지 않습니다.

힌트: 열기, 읽기, 쓰기, _open, _read, _write

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>

int main()
{
	int handle;
	char s() = "abcdefghijk";
	handle = open("write.txt", O_WRONLY | O_CREAT | O_TEXT | O_TRUNC);
	if (handle == -1)
	{
		perror("Error: ");
		return 1;
	}

	write(handle, s, strlen(s));

	close(handle);
	
	return 0;
}


쓰기: 데이터를 파일에 씁니다.

Visual Studio에서는 _write 함수를 사용해야 합니다.

#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() = "abcdefghijk";
	handle = _open("write.txt", O_WRONLY | O_CREAT | O_TEXT | O_TRUNC);
	if (handle == -1)
	{
		perror("Error: ");
		return 1;
	}

	_write(handle, s, strlen(s));

	_close(handle);
	
	return 0;
}