【发布时间】:2014-12-29 02:22:46
【问题描述】:
好的,我正在学习 C++。我想查看有多少文件存在于班级的银行账户程序中。我这样做的方法是运行一个循环来运行并尝试打开“0000000001.txt”以查看它是否有效,然后是“0000000002.txt”,依此类推。困难的部分是数字有前导零(它需要在“.txt”之前总共有10位数字。
double num_of_accounts() {
double number_of_accounts = 0;
double count = 0;
double testFor = 0000000000;
ofstream ifile;
string filename = "0000000000";
for (count = 0; 0 < /*MAX_NUMBER_OF_ACCOUNTS*/ 5; count++){
ifile.open(filename + ".txt");
if (ifile) { number_of_accounts++; }
// I would like to increment the number in the filename by 1 here
} // END of for (count = 0; 0 < MAX_NUMBER_OF_ACCOUNTS; count++){
return number_of_accounts;
}
或者可能是一种将双精度格式设置为 10 位长,然后使用 to_string() 转换为字符串的方法?我尝试用谷歌搜索它,但我不知道我是否使用了错误的关键字。
如果有帮助,这在 win 控制台上。
谢谢
【问题讨论】:
-
为什么不只统计目录中的文件?
-
我也没学过。
-
将“c++ 读取目录”打入你喜欢的搜索引擎。
-
我会调查,但我想确保它计算特定文件。以防万一我放了一个示例文本文件或另一个文件。谢谢你的建议。
-
对,就这样吧。阅读方向并决定,对于每个文件,是否计算它。
标签: c++ string double increment fstream