전화번호부 처럼 이름 , 전화번호, 이메일 주소와 시간을 파일에 기입하고 번호를 입력하면 그 번호에 해당하는 데이터가 출력되는 예제였다..
전화번호가 음수로 들어오는 것을 예외처리 하기 위해서 int 형이아닌 char 형으로 받아서 - 가 들어왔을 때 예외처리 했던 것 같다..
그리고 전화번호 출력 시 '-'을 넣어서 출력한 것 같다.. 근 반년 전에 썼던 코드인데 기억이 가물가물하다
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <time.h> #include <string.h> #include <iostream> using namespace std; int main() { FILE* fp; try { fopen_s(&fp, "c:\\file\\address.dat", "w"); if (fp == NULL) throw; } catch (...) { cout << "실패" << endl; return -1; } char num[20] = { 0 }; char name[20] = { 0 }; char Pnumber[20] = { 0 }; char address[30] = { 0 }; char c = '\0'; int insertCount = 0; while (true) { printf("번호 이름 전화번호 이메일주소\n"); time_t t1; struct tm t2; time(&t1); localtime_s(&t2, &t1); int year = t2.tm_year + 1900; int mon = t2.tm_mon; int day = t2.tm_wday; int hour = t2.tm_hour; int min = t2.tm_min; try { scanf("%s%s%s%s", &num, &name, &Pnumber, &address); strcat(num, " "); strcat(name, " "); strcat(Pnumber, " "); strcat(address, " "); fputs(num, fp); fputs(name, fp); fputs(Pnumber, fp); fputs(address, fp); for (int i = 0; i < 20; i++) { if (Pnumber[i] == '-') { throw 1; } if (name[i] == '-' || name[i] >= '0') { throw 2; } } insertCount++; } catch (int e) { if (e == 1) { cout << "전화번호에 음수가 입력되었습니다."; return -1; } if (e == 2) { cout << "이름에 숫자가 입력되었습니다."; return -1; } } fprintf(fp, "%d년%d월%d일%d시%d초 \n", year, mon, day, hour, min); cout << "입력중단: q 아닐시 아무거나" << endl; cin >> c; if (c == 'q') break; } fclose(fp); try { fopen_s(&fp, "c:\\file\\address.dat", "r"); if (fp == NULL) throw 0; } catch (int temp) { cout << "실패" << endl; return temp; } char k[100] = { 0 }; int n; cout << "출력할 줄번호:"; try { cin >> n; if (n > insertCount) throw 0; } catch (int e) { cout << "그 줄에는 데이터가 없습니다." << endl; return e; } char c2; int count = 0; int p = 1; while (!feof(fp)) { if (n != p) { fgets(k, 100, fp); p++; continue; } c2 = fgetc(fp); if (c2 == ' ') count++; if (count == 2 || count == 4) { int hipoon = 0; while ((c2 = fgetc(fp)) != ' ') { cout << c2; hipoon++; if (count == 2 && (hipoon == 3 || hipoon == 7)) { cout << '-'; } } cout << " "; count++; } } fclose(fp); } |
그
'시스템프로그래밍' 카테고리의 다른 글
이벤트를 사용한 예제 (0) | 2018.05.26 |
---|---|
뮤텍스와 크리티컬섹션을 이용한 기본 예제 (0) | 2018.05.26 |
크리티컬 섹션과 파일입출력을 통한 기본예제.. (0) | 2018.05.26 |
크리티컬 섹션을 이용한 기본예제.. (0) | 2018.05.26 |
멀티프로세스 기본예제 (0) | 2018.05.26 |