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

2023. 5. 19. 00:42ยทํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++ ๊ธฐ์ดˆ

 

 

์ €๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ํ‘œ์ค€์ž…์ถœ๋ ฅ์ด ๋ฌด์—‡์ธ์ง€ ๊ทธ๋ฆฌ๊ณ  ํ‘œ์ค€์ž…์ถœ๋ ฅ๊ณผ ํŒŒ์ผ์ž…์ถœ๋ ฅ์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ดค๋‹ค. ์ „ํ†ต์ ์ธ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— 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

 

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด > C++ ๊ธฐ์ดˆ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[C++] 15. ํด๋ž˜์Šค(class)์™€ ๊ตฌ์กฐ์ฒด(struct)  (0) 2023.06.09
[C++] 14. ์ฐธ์กฐ์ž(Reference)  (1) 2023.06.02
[C++] 13-1. ์ž…์ถœ๋ ฅ(feat. ํ‘œ์ค€์ž…์ถœ๋ ฅ๊ณผ ํŒŒ์ผ์ž…์ถœ๋ ฅ)  (2) 2023.05.17
[C++] 12. ๋ฌธ์ž์—ด(feat. ์ง๋ ฌํ™”)  (0) 2023.05.06
[C++] 11. ํ•จ์ˆ˜(feat. ์ „์—ญํ•จ์ˆ˜, ๋ฉค๋ฒ„ํ•จ์ˆ˜, ์ •์ ํ•จ์ˆ˜, ๋žŒ๋‹คํ•จ์ˆ˜)  (0) 2023.04.28
'ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++ ๊ธฐ์ดˆ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [C++] 15. ํด๋ž˜์Šค(class)์™€ ๊ตฌ์กฐ์ฒด(struct)
  • [C++] 14. ์ฐธ์กฐ์ž(Reference)
  • [C++] 13-1. ์ž…์ถœ๋ ฅ(feat. ํ‘œ์ค€์ž…์ถœ๋ ฅ๊ณผ ํŒŒ์ผ์ž…์ถœ๋ ฅ)
  • [C++] 12. ๋ฌธ์ž์—ด(feat. ์ง๋ ฌํ™”)
์„œ์•„๋ž‘๐Ÿ˜ƒ
์„œ์•„๋ž‘๐Ÿ˜ƒ
Just Do It๐Ÿ’ช
  • ์„œ์•„๋ž‘๐Ÿ˜ƒ
    G-Stack
    ์„œ์•„๋ž‘๐Ÿ˜ƒ
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ์ „์ฒด๋ณด๊ธฐ (144)
      • ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด (78)
        • C++ ๊ธฐ์ดˆ (28)
        • C++ ์‘์šฉ (18)
        • Python (18)
        • JavaScript & NodeJS (0)
        • Go (12)
        • React & NextJS (2)
        • Java (0)
      • AI (2)
      • ์ปดํ“จํ„ฐ ๊ตฌ์กฐ & ์šด์˜์ฒด์ œ (31)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (12)
      • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค (5)
      • ๋„คํŠธ์›Œํฌ (3)
      • ๋””์ž์ธํŒจํ„ด (5)
      • ์„œ๋น„์Šค & ํˆด (7)
      • ํŠธ๋ Œ๋“œ&์ด์Šˆ (1)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

    • G์Šคํƒ์˜ ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ
  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    fork
    ํ•˜๋“œ๋””์Šคํฌ
    STD
    ์ƒ์†
    go
    ์ปดํ“จํ„ฐ
    ๋ณ€์ˆ˜
    ์žฌ๊ท€
    ํŒŒ์ด์ฌ
    ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
    c++
    Thread
    ๊ฐ€์ƒ๋ฉ”๋ชจ๋ฆฌ
    ์กฐ๊ฑด๋ฌธ
    ํŒŒ์ผ์ž…์ถœ๋ ฅ
    RAM
    ๋””์ž์ธํŒจํ„ด
    ์•Œ๊ณ ๋ฆฌ์ฆ˜
    ํ•จ์ˆ˜
    ๋ฉ”๋ชจ๋ฆฌ
    component
    c
    ๋ฐฐ์—ด
    pointer
    ๋ฐ˜๋ณต๋ฌธ
    ํฌ์ธํ„ฐ
    cpu
    ํŒจํ‚ค์ง€
    ์Šคํƒ
    init
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.6
์„œ์•„๋ž‘๐Ÿ˜ƒ
[C++] 13-2. ๊ธฐ๋ณธ์ž…์ถœ๋ ฅ๊ณผ ํŒŒ์ผ์ž…์ถœ๋ ฅ(feat. iostream, ifstream, ofstream, stringstream)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”