๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++ ๊ธฐ์ดˆ

[C++] 13-2. ๊ธฐ๋ณธ์ž…์ถœ๋ ฅ๊ณผ ํŒŒ์ผ์ž…์ถœ๋ ฅ(feat. iostream, ifstream, ofstream, stringstream)

by ์„œ์•„๋ž‘๐Ÿ˜ 2023. 5. 19.

 

 

์ €๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ํ‘œ์ค€์ž…์ถœ๋ ฅ์ด ๋ฌด์—‡์ธ์ง€ ๊ทธ๋ฆฌ๊ณ  ํ‘œ์ค€์ž…์ถœ๋ ฅ๊ณผ ํŒŒ์ผ์ž…์ถœ๋ ฅ์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ดค๋‹ค. ์ „ํ†ต์ ์ธ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— C++์ด ์•„๋‹Œ C ์ฝ”๋“œ๊ฐ€ ๋Œ€๋ถ€๋ถ„์ด์—ˆ๋‹ค. ๋ชจ๋˜ C++์ด ๋‚˜์˜ค๋ฉด์„œ Standard library์—์„œ๋Š” iostream, ifstream, ofstream, stringstream๋“ฑ์˜ stream ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ง€์›ํ•œ๋‹ค. ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด์ž.

 


๊ธฐ๋ณธ ์ž…์ถœ๋ ฅ (Standard Input/Output)


C++์—์„œ ๊ธฐ๋ณธ ์ž…์ถœ๋ ฅ์€ iostream ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. iostream์€ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ (istream)๊ณผ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ (ostream)์„ ์ œ๊ณตํ•œ๋‹ค.

 

istream (์ž…๋ ฅ ์ŠคํŠธ๋ฆผ)

`istream`์€ ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๋Š” ์ŠคํŠธ๋ฆผ์ด๋‹ค. ํ‚ค๋ณด๋“œ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ๋ฒ•์€ std::cin์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

#include <iostream>

int main() {
    int number;
    std::cout << "์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”: ";
    std::cin >> number;
    std::cout << "์ž…๋ ฅ๋œ ์ˆซ์ž: " << number << std::endl;

    return 0;
}


์œ„ ์ฝ”๋“œ์—์„œ std::cin์€ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ์ž…๋ ฅ๋œ ์ˆซ์ž๋Š” number ๋ณ€์ˆ˜์— ์ €์žฅ๋˜๊ณ , ์ดํ›„ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์ธ std::cout์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ๋œ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

ostream (์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ)

ostream์€ ํ‘œ์ค€ ์ถœ๋ ฅ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ŠคํŠธ๋ฆผ์ด๋‹ค. ์ฃผ๋กœ ์ฝ˜์†”์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์‚ฌ์šฉ๋ฒ•์€ std::cout์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

#include <iostream>

int main() {
    int number = 42;
    std::cout << "์ˆซ์ž: " << number << std::endl;

    return 0;
}


์œ„ ์ฝ”๋“œ์—์„œ std::cout์€ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. << ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” "์ˆซ์ž: "์™€ number ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ , ๊ฐœํ–‰ ๋ฌธ์ž std::endl์„ ํ†ตํ•ด ์ค„์„ ๋ฐ”๊พผ๋‹ค.

 

 

 

ํŒŒ์ผ ์ž…์ถœ๋ ฅ (File Input/Output)


C++์—์„œ ํŒŒ์ผ ์ž…์ถœ๋ ฅ์€ fstream ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. fstream๋Š” ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๊ฑฐ๋‚˜ ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ธ ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. fstream๋Š” ifstream (์ž…๋ ฅ ํŒŒ์ผ ์ŠคํŠธ๋ฆผ)์™€ ofstream (์ถœ๋ ฅ ํŒŒ์ผ ์ŠคํŠธ๋ฆผ) ๋‘ ๊ฐ€์ง€ ํด๋ž˜์Šค๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.

 

ifstream (์ž…๋ ฅ ํŒŒ์ผ ์ŠคํŠธ๋ฆผ)

ifstream์€ ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๋Š” ์ŠคํŠธ๋ฆผ์ด๋‹ค. ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ์„ ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ๋‹ค์Œ์€ ํŒŒ์ผ์—์„œ ์ˆซ์ž๋ฅผ ์ฝ์–ด์™€ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๋‹ค.

#include <fstream>
#include <iostream>

int main() {
    std::ifstream file("data.txt");
    if (file.is_open()) {
        int number;
        while (file >> number) {
            std::cout << "์ฝ์€ ์ˆซ์ž: " << number << std::endl;
        }
        file.close();
    } else {
        std::cout << "ํŒŒ์ผ์„ ์—ด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค." << std::endl;
    }

    return 0;
}


์œ„ ์ฝ”๋“œ์—์„œ std::ifstream๋Š” data.txt ํŒŒ์ผ์„ ์—ด์–ด file ๊ฐ์ฒด๋กœ ์‚ฌ์šฉํ•œ๋‹ค. is_open() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ์ด ์ •์ƒ์ ์œผ๋กœ ์—ด๋ ธ๋Š”์ง€ ํ™•์ธํ•œ ํ›„, ํŒŒ์ผ์—์„œ ์ˆซ์ž๋ฅผ ์ฝ์–ด์™€ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

ofstream (์ถœ๋ ฅ ํŒŒ์ผ ์ŠคํŠธ๋ฆผ)

ofstream์€ ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ŠคํŠธ๋ฆผ์ด๋‹ค. ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. ๋‹ค์Œ์€ ์ˆซ์ž๋ฅผ ํŒŒ์ผ์— ์ €์žฅํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค.

#include <fstream>
#include <iostream>

int main() {
    std::ofstream file("data.txt");
    if (file.is_open()) {
        int number = 42;
        file << "์ˆซ์ž: " << number << std::endl;
        file.close();
    } else {
        std::cout << "ํŒŒ์ผ์„ ์—ด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค." << std::endl;
    }

    return 0;
}


์œ„ ์ฝ”๋“œ์—์„œ std::ofstream๋Š” data.txt ํŒŒ์ผ์„ ์—ด์–ด file ๊ฐ์ฒด๋กœ ์‚ฌ์šฉํ•œ๋‹ค. is_open() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ์ด ์ •์ƒ์ ์œผ๋กœ ์—ด๋ ธ๋Š”์ง€ ํ™•์ธํ•œ ํ›„, << ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ์— ์ €์žฅํ•œ๋‹ค.

 

 

stringstream

stringstream์€ ๋ฌธ์ž์—ด์„ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค์ด๋‹ค. stringstream์€ istream๊ณผ ostream์˜ ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋‹ค์Œ์€ stringstream์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์˜ˆ์ œ๋‹ค.

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string input = "42";
    std::stringstream ss(input);

    int number;
    ss >> number;
    std::cout << "๋ณ€ํ™˜๋œ ์ˆซ์ž: " << number << std::endl;

    return 0;
}


์œ„ ์ฝ”๋“œ์—์„œ std::stringstream์€ input ๋ฌธ์ž์—ด์„ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. >> ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์™€ number ๋ณ€์ˆ˜์— ์ €์žฅํ•œ ํ›„, ์ถœ๋ ฅํ•œ๋‹ค.

์ด์ƒ์œผ๋กœ C++์˜ Standard Library๋ฅผ ์‚ฌ์šฉํ•œ ๊ธฐ๋ณธ ์ž…์ถœ๋ ฅ๊ณผ ํŒŒ์ผ ์ž…์ถœ๋ ฅ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. istream, ostream, ifstream, ofstream, stringstream์„ ํ™œ์šฉํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์˜ ์ž…์ถœ๋ ฅ์„ ๋‹ค์–‘ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋” ๋งŽ์€ ๊ธฐ๋Šฅ๊ณผ ์‚ฌ์šฉ๋ฒ•์„ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด C++ ๋ฌธ์„œ๋‚˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด๋ณด๊ธธ ๋ฐ”๋ž€๋‹ค.

 

https://en.cppreference.com/w/cpp/io/basic_ifstream

 

std::basic_ifstream - cppreference.com

The class template basic_ifstream implements high-level input operations on file-based streams. It interfaces a file-based streambuffer (std::basic_filebuf) with the high-level interface of (std::basic_istream). A typical implementation of std::basic_ifstr

en.cppreference.com

https://en.cppreference.com/w/cpp/io/basic_ofstream

 

std::basic_ofstream - cppreference.com

The class template basic_ofstream implements high-level output operations on file based streams. It interfaces a file-based streambuffer (std::basic_filebuf) with the high-level interface of (std::basic_ostream). A typical implementation of std::basic_ofst

en.cppreference.com

https://en.cppreference.com/w/cpp/io/basic_stringstream

 

std::basic_stringstream - cppreference.com

template<     class CharT,     class Traits = std::char_traits ,     class Allocator = std::allocator > class basic_stringstream : public basic_iostream ; The class template std::basic_stringstream implements input and output operations on string bas

en.cppreference.com

 

๋Œ“๊ธ€