메모내용

FileSystem

fwrite

김성엽 :: fwrite

fopen, fwrite, fread 함수 정리 하자

FileSystem 의 추가 방법

C++ 17 에서 파일시스템이 추가 되었다.
프로젝트 속성 -> 일반 -> c++ 언어표준 = c++ 17 로 설정한다

헤더 파일 추가

                
                    // 기존의 byte 와 c++17 에서의 std::byte 의 충돌문제 방지
                    // using namespace std; 를 삭제하거나 아래의 방법으로 충돌을 방지할 수 있다.
                    // 위쪽에 적어준다
                    #deifne _HAS_STD_BYTE 0         

                    #include <fileSystem>
                        
                    namespace fs = std::fileSystem;
    
                    // 확장자 추출하기
                    wstring ext = fs::path(/* 파일경로 */path).extension();
                
            
                
#pragma once
#include "pch.h"

#include <iostream>
#include <filesystem>

#include <fstream>

#include <cwchar>


int main()
{

	std::filesystem::path dirPath = "Intermediate/data";
	std::filesystem::path filePath = dirPath;
	filePath += "/OutputData_1.txt";

	if (std::filesystem::exists(dirPath))
	{
		std::cout << "exist directory" << std::endl;

		if (std::filesystem::exists(filePath))
		{
			std::ofstream OutFile(filePath.wstring());

			OutFile << "write file in already exist file";

			OutFile.close();
			
			std::cout << "success write to exist file" << std::endl;
		}
		else {
			std::cout << "no exist file \"OutputData_1.txt\"" << std::endl;

			std::ofstream OutFile(filePath.wstring());

			OutFile << "write new file in directory";

			OutFile.close();

			std::cout << "success file in exist directory" << std::endl;
		}
	}
	else {
		std::cout << "no directory" << std::endl;

		if (std::filesystem::create_directories(dirPath))
		{
			std::cout << "create file has successed" << std::endl;

			
			dirPath += "/OutputData_1.txt";

			std::ofstream OutFile(dirPath.wstring());

			OutFile << "make directory and write file";
			OutFile.close();

			std::cout << "write data is successful" << std::endl;

		}
		else {
			std::cout << "create file failed" << std::endl;
		}
	}


	return 0;

}