我开始认为这完全不可能......
我有一个小型控制台程序,它基本上是一个小型计算器,一旦用户完成一次计算并获得结果,程序就会循环以允许用户进行另一次计算。只要用户继续选择添加另一个计算,程序就永远不会结束。
现在我想要的是能够将控制台中显示的所有数据保存到桌面上的新文本文件中。
我有一组函数,它们将完整的文件路径和文件名声明为字符串,如果可能的话,我希望程序将文本文件保存到这个确切的文件路径和名称。如果不可能,那么用户的桌面就可以了。
到目前为止,我已经尝试了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;
}
答案 0 :(得分:0)
使用ofstream,ofstream consoleCopy; consoloeCopy.open(filepath);
其中filepath是您使用函数生成的路径。然后,只需写入文件consoleCopy << consoleString;
即可。
要实现您想要的功能,您需要将所有输出/输入存储到字符串中并将其写入文件末尾,或者随时写入文件。以下网站可以帮助您写入文件http://www.cplusplus.com/doc/tutorial/files/
答案 1 :(得分:0)
在C ++ 98中ofstream open()的形成是:
void open (const char* filename, ios_base::openmode mode = ios_base::out);
您的pathName是字符串类型。复制char数组中的pathName值并在open()方法中传递它在我的电脑上工作。
所以我做了(!!!),声明了一个char数组并将pathName复制到该数组中并在open()中使用它。
char pname[100] = {'\0'};
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) ____________________________________________________________
不要忘记包含“#include&lt; fstream&gt;”在标题