티스토리 뷰

728x90
SMALL

텍스트 데이터와 바이너리 데이터를 동시에 입출력하기

서식에 따른 데이터 입출력 : frpintf, fscanf

fscanf는 파일의 끝에 도달하거나 오류가 발생하면 EOF를 반환한다.

쓰기

#include <stdio.h>

int main(void)
{
	char name[10];
	char sex;
	int age;

	FILE* fp;
	fopen_s(&fp,"friend.txt", "wt");
	int i;

	for (i = 0; i < 3; i++)
	{
		printf("이름 성별 나이 순 입력: ");
		scanf_s("%s %c %d", name,10, &sex,1, &age);
		getchar(); //버퍼에 남아있는 \n의 소멸을 위해서
		fprintf(fp, "%s %c %d", name, sex, age);
	}
	fclose(fp);
	return 0;
}

읽기

#include <stdio.h>

int main(void)
{
	char name[10];
	char sex;
	int age;

	FILE* fp;
	fopen_s(&fp,"friend.txt", "rt");
	int ret;

	while (1)
	{
		ret = fscanf_s(fp, "%s %c %d", name,10, &sex,1, &age);
		if (ret == EOF)
			break;
		printf("%s %c %d \n", name, sex, age);
	}
	fclose(fp);
	return 0;
}

 구조체

#include <stdio.h>
typedef struct fren
{
	char name[10];
	char sex;
	int age;
} Friend;

int main(void)
{
	FILE* fp;
	Friend myfren1;
	Friend myfren2;

	/*** file write ***/
	fopen_s(&fp,"friend.bin", "wb");
	printf("이름, 성별, 나이 순 입력: ");
	scanf_s("%s %c %d", myfren1.name,10, &(myfren1.sex),1, &(myfren1.age));
	fwrite((void*)&myfren1, sizeof(myfren1), 1, fp);
	fclose(fp);

	/*** file read ***/
	fopen_s(&fp,"friend.bin", "rb");
	fread((void*)&myfren2, sizeof(myfren2), 1, fp);
	printf("%s %c %d \n", myfren2.name, myfren2.sex, myfren2.age);
	fclose(fp);
	return 0;
}

접근을 위한 파일 위치 지시자의 이동

파일의 중간 또는 마지막 부분에 저장된 데이터의 일부를 읽어야 하는 경우 파일 위치 지시자를 이용

 

파일 위치 지시자의 이동 : fseek

#include <stdio.h>

int fseek(FILE * stream, long offset, int wherefrom);

  => 성공 시 0, 실패 시 0이 아닌 값을 반환

stream으로 전달된 파일 위치 지시자를 wherefrom에서부터 offset 바이트만큼 이동시켜라

매개변수 wherefrom 이 파일 위치 지시자는
SEEK_SET(0)이라면 파일 맨 앞에서부터 이동을 시작
SEEK_CUR(1)이라면 현재 위치에서부터 이동을 시작
SEEK_END(2)이라면 파일 맨 끝에서부터 이동을 시작

여기서 파일의 끝은 파일의 마지막 데이터가 아닌, 파일의 끝을 표시하기 위해서 삽입이 되는 EOF를 의미

#include <stdio.h>

int main(void)
{
	/* 파일생성 */
	FILE* fp;
	fopen_s(&fp,"text.txt", "wt");
	fputs("123456789", fp);
	fclose(fp);

	/* 파일개방 */
	fopen_s(&fp,"text.txt", "rt");

	/* SEEK_END test */
	fseek(fp, -2, SEEK_END); //EOF에서 앞으로 2칸, 8
	putchar(fgetc(fp));

	/* SEEK_SET test */
	fseek(fp, 2, SEEK_SET); //처음에서 뒤로 2칸, 3. 파일 위치 지시자는 4
	putchar(fgetc(fp));

	/* SEEK_CUR test */
	fseek(fp, 2, SEEK_CUR); //현재(4)에서 뒤로 2칸, 6
	putchar(fgetc(fp));

	fclose(fp);
	return 0;
}
// 836

현재 파일 위치 지시자의 위치는? : ftell

#include <stdio.h>

long ftell(FILE * stream);

  => 파일 위치 짓시자의 위치 정보 반한

#include <stdio.h>

int main(void)
{
	long fpos;
	int i;

	/* 파일생성 */
	FILE* fp;
	fopen_s(&fp,"text.txt", "wt");
	fputs("1234-", fp);
	fclose(fp);

	/* 파일개방 */
	fopen_s(&fp,"text.txt", "rt");

	for (i = 0; i < 4; i++)
	{
		putchar(fgetc(fp));
		fpos = ftell(fp);
		fseek(fp, -1, SEEK_END);
		putchar(fgetc(fp));
		fseek(fp, fpos, SEEK_SET);
	}
	fclose(fp);
	return 0;
}
// 1-2-3-4

 

#include <stdio.h>
#include <string.h>

long GetFileSize(FILE* fp);

int main(void)
{
	char str[100];
	FILE* fp;
	fopen_s(&fp, "ABC.txt", "rt");
	fgets(str, 100, fp);
	fputs(str, stdout);
	printf("파일의 크기 : %ld \n", GetFileSize(fp));
	fgets(str, 100, fp);
	fputs(str, stdout);
	printf("파일의 크기 : %ld \n", GetFileSize(fp));
	fgets(str, 100, fp);
	fputs(str, stdout);
	fclose(fp);
	return 0;
}

long GetFileSize(FILE* fp) {
	long fpos;
	long fsize;
	fpos = ftell(fp); //파일 위치 지시자 정보 백업

	fseek(fp, 0, SEEK_END);
	fsize = ftell(fp);
	fseek(fp, fpos, SEEK_SET); // 파일 위치 지시자 정보 복구
	return fsize;
}
728x90
LIST
댓글
공지사항