[Multi-Core & GPU Programming] HW0 - Test Server Account & Read & Print File Contents
"Test Server Account & Read & Print File Contents"
[Objective]
과제 수행을 위한 계정을 생성하고, 파일을 읽고 해당 파일 안의 내용을 거꾸로 출력하여 C++ 이해하기
[Assignment Description]
· Server Address = 165.132.46.80 / Port Number = 31522
[Solve The Assignment]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> #include <fstream> #include <string> int main() { std::ifstream input_file("/data/hw0/data.txt"); // Open File if (input_file.is_open() == 0) { // Fail To Open File std::cout << "Error!" << std::endl; // Print Error Message return 0; // End The Program } std::string data; // Declare Variable To Store "input_file" Whole Data char c; // Declare Variable To Read One Letter Of "input_file" while (input_file.get(c)) { // Get One By One data += c; // Store In "data" } for (int i = data.length() - 1; i >= 0; i--) { // Start Loop From Behind Of Data std::cout << data[i]; // Print Data } std::cout << "\n"; // New Line input_file.close(); // Close File return 0; } | cs |
6 : 표준 Library "std" 안에 있는 ifstream을 사용하여 "input_file" 이름으로 File Open
→ Windows 환경 : "C:\\Users\\name\\file.txt"
→ Linux/Unix 환경 : "/data/hw0/data.txt"
8 : File Open 실패 시 "~.is_open() == 0"
9 : Error 메세지 출력
→ std::cout << "Error!\n"도 가능하나 Error 발생 시 Program 자체가 끝나므로 줄도 바꾸고 Buffer도 비우는 std::endl 사용
10 : Program 종료
13 : input_file에서 읽은 데이터를 저장할 변수 "data" 선언
14 : input_file에서 한 글자씩 읽는 변수 "c" 선언
16 : End Of File에서는 읽을 글자가 없으므로 조건이 False가 됨
17 : 한 글자씩 읽어서 "data"에 저장
20 : Index를 뒤에서부터 수행
21 : 해당 Index의 값을 출력
24 : 출력 종료 후, 마지막 줄 바꿈
26 : input_file 닫기
27 : Program 종료
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> #include <fstream> #include <string> int main() { std::ifstream input_file("/data/hw0/data.txt"); // Open File if (input_file.is_open() == 0) { // Fail To Open File std::cout << "Error!" << std::endl; // Print Error Message return 0; // End The Program } std::string data; std::string line; while (std::getline(input_file,line)) { data += line + "\n"; } for (int i = data.length() - 1; i >= 0; i--) { // Start Loop From Behind Of Data std::cout << data[i]; // Print Data } std::cout << "\n"; // New Line input_file.close(); // Close File return 0; } | cs |
· 위와 같이 char c → get(c) → data += c 대신 std::string line → std::getline(input_file, line) → data += line + "\n" 가능
** 파일의 맨 마지막에 "\n"이 없는 경우
→ getline으로 파일 데이터를 불러오면 각 줄마다 "\n"을 추가
→ "\n"이 없는 맨 마지막 줄도 "\n" 추가
→ 거꾸로 출력 시 파일과 달리 "\n"이 먼저 출력
∴ getchar 방법이 더 안전
** Assignment의 설명을 보면 "You are required to read a string from a file …"로 되어 있으므로 아래와 같이 작성 가능
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> #include <fstream> #include <string> int main() { std::ifstream input_file("/data/hw0/data.txt"); // Open File if (input_file.is_open() == 0) { // Fail To Open File std::cout << "Error!" << std::endl; // Print Error Message return 0; // End The Program } std::string data; // Declare Variable To Store "input_file" Data std::getline(input_file, data); // Get A String From "input_file" for (int i = data.length() - 1; i >= 0; i--) { // Start Loop From Behind Of Data std::cout << data[i]; // Print Data } std::cout << "\n"; // New Line input_file.close(); // Close File return 0; } | cs |
· 파일에 한 줄의 문장 밖에 없기 때문에 while 구문 없이 getline을 통해 "data" 변수로 바로 받음
[Reference]
· HW0 - Test Server (MGP) - Yongjun Park
댓글
댓글 쓰기