我现在已经加入了#include <sstream>
。这有帮助。但仍然是显示正确信息的问题。我已经把它搞定了,它必须处理某种程序错误。我已经在同一个文件夹路径中创建了文本文件。我已经尝试将其设置为直接C:/路径,例如
C:\ Users \ Matt \ Documents \ Visual Studio 2015 \ Projects \ listofcolors.txt
我在打开文件和显示数据时遇到问题。我最近下载了Microsoft Visual Basic C ++来编写Mastermind所需的程序。我的目标是创建代表9到5的数字并将它们放在文本文件中。 (基本上,5列9个数字,9我到目前为止写了这个。
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main() {
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
std::string result;
std::stringstream sstm;
ofstream myfile;
myfile.open("listofcolors.txt");
if (myfile.is_open())
{
for (a = 0; a<9; a++) {
sstm << a << b << c << d << e;
result = sstm.str();
myfile << result << '\n';
for (b = 0; b<9; b++) {
sstm << a << b << c << d << e;
result = sstm.str();
myfile << result << '\n';
for (c = 0; c<9; c++) {
sstm << a << b << c << d << e;
result = sstm.str();
myfile << result << '\n';
for (d = 0; d<9; d++) {
sstm << a << b << c << d << e;
result = sstm.str();
myfile << result << '\n';
for (e = 0; e<9; e++) {
sstm << a << b << c << d << e;
result = sstm.str();
myfile << result << '\n';
}
}
}
}
myfile.close();
return 0;
}
}
else cout << "Unable to open file";
return 0;
}
由于某种原因,没有输出任何内容。有任何想法吗???谢谢!!
答案 0 :(得分:0)
我们可以通过借用将在基数9中输出数字的this arbitrary base conversion code来变得聪明 - 请参阅cout
version online here:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
using namespace std;
/**
* C++ version 0.4 std::string style "itoa":
* Contributions from Stuart Lowe, Ray-Yuan Sheu,
* Rodrigo de Salvo Braz, Luc Gallant, John Maloney
* and Brian Hunt
*/
std::string itoa(int value, int base) {
std::string buf;
// check that the base if valid
if (base < 2 || base > 16) return buf;
enum { kMaxDigits = 35 };
buf.reserve( kMaxDigits ); // Pre-allocate enough space.
int quotient = value;
// Translating number to string with base:
do {
buf += "0123456789abcdef"[ std::abs( quotient % base ) ];
quotient /= base;
} while ( quotient );
// Append the negative sign
if ( value < 0) buf += '-';
std::reverse( buf.begin(), buf.end() );
return buf;
}
int main() {
ofstream myfile("listofcolors.text");
if (myfile.is_open())
{
for (int i = 0; i <= 9 * 9 * 9 * 9 * 9; i++)
{
// Formats the number to five places, filled with leading zeroes
myfile << setw(5) << setfill('0') << itoa(i, 9) << '\n';
}
myfile.close();
}
else
cout << "Unable to open file";
return 0;
}
请注意,该文件是在工作目录中创建的;这是how to check and set where it is。
另请注意,您的代码在第一个循环中包含myfile.close()
,因此它只会从00000
打印到08888
。
最后,由于您使用的是Windows,因此请使用文件资源管理器搜索listofcolors.txt
,以防它在意外位置创建。
答案 1 :(得分:0)
在Windows上,文本文件的文件扩展名为.txt
,而不是.text
。此外,您正在使用相对路径创建文件,因此它将在进程的当前工作目录中创建,这可能不是您期望的那样。始终使用绝对路径。
你的循环代码不必要地复杂,它可以简化为更像这样的东西:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream myfile("C:\\some path\\listofcolors.txt", ios_base::trunc);
if (myfile.is_open())
{
for (int a = 0; a < 9; a++)
for (int b = 0; b < 9; b++)
for (int c = 0; c < 9; c++)
for (int d = 0; d < 9; d++)
for (int e = 0; e < 9; e++) {
if (!(myfile << a << b << c << d << e << '\n')) {
cout << "Unable to write to file";
return 0;
}
}
myfile.close();
cout << "File created";
}
else
cout << "Unable to create file";
return 0;
}