본문 바로가기

College Study/C++ (ENG)23

[C++] File I/O (ENG) File Stream ifstream : input file ofstream : output file fstream : input and output file We can use operator and a manipulator in a file stream. // read only open ifstream fin; // an object fin.open("helloWorld.txt"); // write only open ofstream fout; fout.open("helloWorld.txt"); // read and write fstream fs; fs.open("helloWorld.txt"); open( ) Every stream has open( ) method. fin.open("HelloWorl.. 2022. 1. 2.
[C++] String (ENG) std::string Class A string is not an array, but a class in C++. A string using a std::string class can be increased in length. #include std::string name; std::cin >> name; also possible to assign and append. It is intuitive and secure. string firstName = "Pury"; string fullName = "Onion"; // assignment fullName = firstName; // appending fullName += "Bab"; concatenation: string firstName = "Pury".. 2021. 12. 5.
[C++] New Concepts (ENG) Bool Data if (IsHero() == false) { // ... } if (IsHero() == true) { // ... } Reference Alias. Can not be NULL. Must be initialized when declared. int number = 100; int& reference = number; Can not change what refer to. int number1 = 100; int number2 = 200; int& reference = number1; reference = number2; // number1 == 200, number2 == 200, reference == 200 Reference as a parameter: // pointer void .. 2021. 12. 5.
[C++] Input (ENG) Input Stream scanf has the input problem cause it doesn't do the boundary check, so does cin. For more security: char name[4]; cin >> setw(4) >> name; 0 1 2 3 H W I \0 Stream States Old (C): if (fgets(line, 10, stdin) != NULL) { // ... } New: cin >> line; if (!cin.eof()) { // ... } Istream states - namespace : ios_base - bit flag : goodbit, eofbit, failbit, badbit - method : good(), eof(), fail(.. 2021. 12. 5.
[C++] Output (ENG) "Hello World" std::cout 2021. 12. 5.