C++ 기말고사 준비 (스트림 및 파일 입/출력)

Jaewoong2·2020년 11월 30일
0

C++

목록 보기
9/13
/** 12장 display 12.7 */
void makeNeat(ifstream& messyFile, ofstream& neatFile, int numberAfterDecimalpoint, int fieldWidth);

int main() {

    ifstream fin;
    ofstream fout;

    fin.open("rawdata.txt");
    if(fin.fail()) exit(1);
    fout.open("neat.file");
    if(fout.fail()) exit(1);
    makeNeat(fin, fout, 5, 12);

    fin.close();
    fout.close();

    return 0;
}

// stream 형식은 & 참조형식으로 가져와야한다.
// call by value 는 X
// call by reference 는 O
void makeNeat(ifstream& messyFile, ofstream& neatFile, int numberAfterDecimalpoint, int fieldWidth) {
    // 고정소수점
    neatFile.setf(ios::fixed);
    // 소수점을 보인다
    neatFile.setf(ios::showpoint);
    // 양의 부호 음의 부호를 보인다
    neatFile.setf(ios::showpos);
    // 소수점 아래의 갯수 설정
    neatFile.precision(numberAfterDecimalpoint);

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.setf(ios::showpos);
    cout.precision(numberAfterDecimalpoint);

    double next;
    // messyFile 로 부터 입력을 받는다.
    while (messyFile >> next) {
        cout << setw(fieldWidth) << next << endl;
        // neatFile 에 출력을 한다.
        // width 는 fieldWidth 값을 갖는다. 숫자를 오른쪽 부터 개수를 새서 fieldWidth 갯수와 맞춘다
        // (+ , - 부호도 포함)
        neatFile << setw(fieldWidth) << next << endl;
    }
}
/** display 12.8 */
void addPlusPlus(ifstream& inStream, ofstream& outStream);

 int main() {
     ifstream fin;
     ofstream fout;

     cout << "Begin Editing Files. \n";

     fin.open("cad.txt");
     if(fin.fail()) exit(1);

     fout.open("cppad.txt");
     if(fout.fail()) exit(1);

     addPlusPlus(fin, fout);
     fin.close();
     fout.close();

     return 0;
 }
void addPlusPlus(ifstream& inStream, ofstream& outStream) {
     char next;

//    inStream >> next;
     inStream.get(next);
     while(!inStream.eof()) { // 파일이 끝날 때까지.
         if(next == 'C')
             outStream << "C++";
         else
             outStream << next;

//         inStream >> next;
        inStream.get(next);
        // 공백문자를 읽기 위해서 get 함수 를 사용해야한다.
     }
 }
/** display 12.9 */

int main() {
  stringstream ss;
  string scores = "Luigi 70 100 90";

  ss.str("");
  ss.clear();

  // ss 에 scores 를 입력한다.
  ss << scores;

  string name = "";
  int total = 0, count = 0, average = 0;
  int score;

  // ss 로 부터 name 을 입력 받는다.
  ss >> name;
  while (ss >> score) {
      count++;
      total += score;
  }
  if(count > 0) {
      average = total / count;
  }
  // ss를 초기화 한다.
  ss.clear();
  ss.str("");

  // ss 에 << 뒤로 받는 값들을 입력받는다.
  ss << "Name : " << name << " Average : " << average;

  // ss 에 입력받은 값을 string 값으로 반환한다.
  cout << ss.str() << endl;

  return 0;
};
int main() {
    stringstream ssList, ssNum;
    string numbers = "1.1, 1.2, 1.3";

    double total = 0;
    double num;

    ssList.clear();
    ssList.str(numbers);

    string field;
    while (getline(ssList, field, ',')) {
        // getline 함수는,
        // 첫번째 매게변수에 있는 스트링스트림을 한글자 씩 읽어들여
        // delim 값이 있을 때 실행을 멈추고 그때 까지의 값을 복사해서
        // 2번째 string 매게변수에 복사해서 넣어준다.


        // str() 함수를 사용하면, flag bit 가 1 이 된다.
        // flag bit 가 1 이 되면 str() 로 값을 초기화 하여 받을 수 없다.
        // flag bit 를 0 으로 초기화 하여 str() 로 값을 초기화 하여 받기 위하여 clear() 를 사용한다.


        // 여기에서 clear() 함수를 사용하지 않으면 ssNum 에는 1.1만 입력된 상태로 다른 값을 입력 받지 못한다
        // 즉, 초기화 시키지 못하기 때문에 str() 로 값을 받으려면, 그 전에 claer() 를 사용하여 flag bit를 0으로 해줘야한다.
        ssNum.clear();
        ssNum.str(field);
        cout << ssNum.str() << endl;
        ssNum >> num;
        total += num;
    }
    cout << total << endl;
}
profile
DFF (Development For Fun)

0개의 댓글