Language/C & C++

[C]File Input & Ouptut

Rogue 2022. 10. 22. 17:25
반응형
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
	FILE* fp = fopen("C:\\Users\\hrmk\\OneDrive\\바탕 화면\\text\\data.txt", "rt");
	if (fp == NULL)
	{
		puts("Failed to open file.");
		return -1;
	}

	//for (int i = 0; i < 3; i++)
	//{
	//	int ch = fgetc(fp);
	//	printf("%c\n", ch);
	//}

	int ch;
	char str[30];

	ch = fgetc(fp);
	printf("%c\n", ch);
	ch = fgetc(fp);
	printf("%c \n", ch);

	fgets(str, sizeof(str), fp);
	printf("%s", str);
	fgets(str, sizeof(str), fp);
	printf("%s", str);
	
	//fputc('A', fp);
	//fputc('B', fp);
	//fputs("My name is Jake\n", fp);
	//fputs("Your name is Mike\n", fp);

	fclose(fp);

	return 0;
}
반응형