将所有控制台输出数据保存到新的文本文件 C++

Save all console out data to a new text file C++

我开始觉得这完全不可能...

我有一个小型控制台程序,它基本上是一个小型计算器,一旦用户进行了一次计算并获得了结果,程序就会循环以允许用户进行另一次计算。只要用户继续选择添加另一个计算程序就永远不会结束。

现在我想要的是能够将控制台中显示的所有数据保存到桌面上的新文本文件中。

我有一组函数将完整的文件路径和文件名声明为字符串,如果可能的话,我希望程序将文本文件保存到这个确切的文件路径和名称中。如果不可能,那么用户的桌面也可以。

到目前为止,我已经尝试过 Fstream 但没有成功;也许我做错了什么?我对这一切有点陌生,所以任何帮助将不胜感激。

以下是程序中的所有代码。

#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <windows.h>
#include <Lmcons.h>
using namespace std;

int main()
{
   //creates file name
    char timeNow[20];
    time_t now = time(NULL);
    strftime(timeNow, 20, "%d.%m.%Y %H%Mhrs", localtime(&now));

// Creates desktop file path that includes the users user name
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);
stringstream user;
string UserName;
user << username;
user >> UserName;
string pathName = "c:/users/" + UserName + "/desktop/" + timeNow + ".txt";

// Declare Variables
int suA, splitA, cornerA, streetA, sixlineA, payout;
double Cvalue = 1;
double cash;
int winnum = 0;
int table = 0;
std::string Another ("y");

// Programme Header
cout << "Roulette Bet Calculator & Payout Log!" << endl;
cout << "By Chris McCarthy" << endl << endl;

    //Bet Calculator Loop
    while (Another == "y") {
    cout << "Table no: AR";
    cin >> table;
    cout << "Winning Number? ";
    cin >> winnum;
    char timeNow[20];
    time_t now = time(NULL);
    strftime(timeNow, 20, "%H:%M:%S %d/%m/%Y", localtime(&now));
    cout << timeNow << endl << endl;
    cout << "Please enter the chip value? " << char(156);
    cin >> Cvalue;
    cout << "Please enter the amount of Straight Ups? ";
    cin >> suA;
    cout << "Please enter the amount of Splits? ";
    cin >> splitA;
    cout << "Please enter the amount of Corners? ";
    cin >> cornerA;
    cout << "Please enter the amount of Streets? ";
    cin >> streetA;
    cout << "Please enter the amount of Six Lines? ";
    cin >> sixlineA;
    cout << endl;

    // Calculates then writes the final payout and cash value
    payout  = (suA * 35) + (splitA * 17) + (cornerA * 8) + (streetA * 11) + (sixlineA * 5);
    cash = Cvalue * payout;
    cout << "The payout is: " << payout << endl;
    cout << "The cash value of the payout is: " << char(156) << cash << endl << endl;

    // Adds another bet or terminates programme
    cout << "Add another bet? (y/n) ";
    cin >> Another;
    cout << "____________________________________________________________" << endl << endl;
}
return 0;
}

使用 ofstream,ofstream consoleCopy; consoloeCopy.open(filepath); 其中文件路径是您使用函数生成的路径。那么就是写入文件的事情了,consoleCopy << consoleString;.

不过,要实现您想要的效果,您需要将所有 output/input 存储到一个字符串中并在最后将其写入文件,或者在您写入文件时写入文件走。以下站点应该可以帮助您写入文件,http://www.cplusplus.com/doc/tutorial/files/

在 C++98 中 ofstream open() 的形式是:

void open (const char* filename,  ios_base::openmode mode = ios_base::out);

您的路径名是字符串类型。将 pathName 值复制到 char 数组中并将其传递到 open() 方法中在我的电脑上工作。

所以我做了 (!!!),声明了一个 char 数组并将 pathName 复制到该数组中并在 open() 中使用它。

char pname[100] = {'[=11=]'};
for(int i=0; i<sizeof(pname); i++) {
    pname[i] = pathName[i];
}

std::ofstream ofs;
ofs.open (pname, std::ofstream::out | std::ofstream::app);

并且在每个

cout << ... << endl;
之后我输入
ofs << ...<< endl;

它在我的桌面上生成了一个名为“27.04.2016 1643hrs.txt”的文件,我在文件中得到了这些字符串:

Roulette Bet Calculator & Payout Log!
By Chris McCarthy

Table no: ARWinning Number? 16:43:19 27/04/2016

Please enter the chip value? œPlease enter the amount of Straight Ups? Please enter the amount of Splits? Please enter the amount of Corners? Please enter the amount of Streets? Please enter the amount of Six Lines? 
The payout is: 238
The cash value of the payout is: œ238

Add another bet? (y/n) ____________________________________________________________


不要忘记在 header

中包含“#include