아래 콘솔 데이터를 텍스트 파일에 저장해야합니다.
그러나 출력이 배치 될 텍스트 파일의 이름은 사용자가 입력합니다. 요청 된 텍스트 파일에 모든 콘솔 데이터를 저장하는 적절한 방법을 찾을 수 없습니다. 너비와 특수 공간을 사용하기 때문에 어떻게해야할지 잘 모르겠습니다.
내 코드 :
if(message == "5"){
cout << "\n[View Data...]" << endl;
cout << "filtering criteria:" << optionTwo << endl;
cout << "sorting criteria:" << optionThree << endl;
cout << "sorting order:" << optionFour << endl;
cout << "" << endl;
if(optionTwo == "Point2D"){
std::cout.width(5); std::cout << std::right << "X";
std::cout.width(6); std::cout << std::right << "Y";
std::cout.width(9); std::cout << std::right << "Dist.";
std::cout.width(3); std::cout << std::right << "Fr" ;
std::cout.width(7); std::cout << std::right << "Origin" << endl;
}
}
if(message == "6"){
cout << "Please enter filename: " ;
cin >> message6;
}
텍스트를 보낼 때 cout
기본적으로 이미 파일에 씁니다 stdout
. 다른 출력 파일 스트림을 열고 동일한 방식으로 출력을 보내면됩니다.
#include <iostream>
#include <fstream>
using namespace std;
ofstream out = ofstream("file.txt", ios::out);
out.width(5);
out << std:;right << "X";
객체 ios::out
의 기본 모드이기 때문에 를 엄격하게 포함 할 필요는 없습니다 ofstream
. cppreference 에서 더 많은 것을 읽을 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다