如何将一个文件递增十次?它涉及(i = 0,i< 10,i ++)。我遇到的大部分内容都是关于循环整数或打开多个不同的文件。我正在使用的代码如下。谢谢你的帮助。
const char *path="/Desktop/Libraries/Documents" ;
ofstream myfile(path);
myfile.open ("howyoulike.doc") ;
myfile << "How you like math\n" ;
myfile.close();
所以,我编写了我的代码:
const char *path="/Desktop/Libraries/Documents" ;
const char * FILENAMES[] ={"file1.doc", "file2.doc", "file3.doc", "file4.doc", "file5.doc"};
for (i = 0; i < 5; i++)
ofstream myfile(path);
myfile.open (FILENAMES[i]) ;
myfile << "How you like math?\n" ;
myfile.close();
可执行/运行代码,现在是我程序的一部分。谢谢你的帮助!
#include <iostream>
#include <fstream>
using namespace std;
struct pizza{
string FILENAMES[9];
};
int main ()
{
int i;
pizza greg = {"file1.doc", "file2.doc", "file3.doc", "file4.doc", "file5.doc"};
cout << "Input is invalid. Program will end. " << "\n" ;
for (i = 0; i < 5; i++)
{
const char *path="/Desktop/Libraries/Documents" ;
ofstream myfile(path);
myfile.open (greg.FILENAMES[i]) ;
myfile << "How you like math?\n" ;
myfile.close();
};
return 0;
}
答案 0 :(得分:2)
递增文件是什么意思?我可以想象如果你想在文件中增加数据,但文件本身......对我来说没有多大意义?