在循环中创建多个文本文件 我想在C++中创建一些文本文件。例如:我将运行从1到5的循环,并创建以下文件: 1.txt 2.txt 3.txt 4.txt 5.txt
可能吗?我制作了一个示例代码:在循环中创建多个文本文件 我想在C++中创建一些文本文件。例如:我将运行从1到5的循环,并创建以下文件: 1.txt 2.txt 3.txt 4.txt 5.txt,c++,file,C++,File,可能吗?我制作了一个示例代码: #include<iostream> #include<cstdio> #include<cstdlib> using namespace std; main() { FILE *fp; int i; for(i=1;i<=5;i++) { //fp=fopen("%d.txt","r",i); //what will go here?? } } 我对我将在循环中写什么
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
main()
{
FILE *fp;
int i;
for(i=1;i<=5;i++)
{
//fp=fopen("%d.txt","r",i); //what will go here??
}
}
我对我将在循环中写什么感到困惑。如何创建这些文件
char i;
char fileName[] = "0.txt";
for(i='1';i<='5';i++)
{
fileName[0]=i;
fp=fopen(fileName,"r"); //what will go here??
//...
}
如果你仍然不需要字符串,你没有c++11或者你的编译器没有这个,你现在可以使用这个简单的版本。最好将其放在您自己的名称空间中
如果你仍然不需要字符串,你没有c++11或者你的编译器没有这个,你现在可以使用这个简单的版本。最好将其放在您自己的名称空间中
在将文件名作为std::string传递给std::ofstream构造函数之前,可以使用组成文件名
在将文件名作为std::string传递给std::ofstream构造函数之前,可以使用组成文件名
***为了能够在打开多个文件时使用一个ostream对象,必须关闭前一个文件才能打开下一个文件,否则它无法创建下一个文件
***要在打开多个文件时使用一个ostream对象,必须关闭前一个文件才能打开下一个文件,否则它无法创建下一个文件。在std::string变量中单独创建文件名,并使用c_str将其传递给fopen。在std::string变量中单独创建文件名,并使用CSTR将它传递给FOpenT。这将不会像我问的那样,如果i>9,因为1到5部分是使用SCAPTF的例子,或者只是使用C++的东西,我把它们添加到我的答案中。apple@AAA您使用的编译器是什么?您使用的是哪种标准?ToStand定义为,但自从C++ 11以来,我使用代码块13.12版本@ BBYYA,这将不会像问到如果i>9’,因为1到5部分是使用SCAPTF或使用C++的例子。我正在将它们添加到我的回答中。我收到一个错误,告诉您字符串在此范围内未被解密@appleapple@AAA您使用的编译器是什么?您使用的是哪种标准?to_字符串在中定义,但从C++11开始。我使用代码块版本13.12@Bob__
#include <fstream>
#include <string>
using namespace std;
int main(){
string base(".txt");
for(int i=1;i<=5;++i){
ofstream(to_string(i)+base);// to_string() need c++11
}
}
#include <string>
#include <sstream>
std::string to_string(int i){
std::stringstream s;
s << i;
return s.str();
}
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
int main()
{
std::cout << "How many files do you want to create? ";
int n;
std::cin >> n;
std::cout << "How many digits do you want to display? ";
int n_digits;
std::cin >> n_digits; // i.e. zeroes == 3 -> 001.txt
std::cout << "Enter a common prefix for all the files: ";
std::string prefix;
std::cin.ignore();
std::getline(std::cin, prefix); // i.e. prefix == "file" -> file001.txt
std::string ext(".txt");
for ( int i = 1; i <= n; ++i )
{ // use a stringstream to create a file names like: prefix001.txt
std::stringstream ss;
ss << prefix << std::setfill('0') << std::setw(n_digits) << i << ext;
// open the file. If not c++11 use ss.str().c_str() instead
std::ofstream file( ss.str() );
if ( !file )
{
std::cerr << "Error: failed to create file " << ss.str() << '\n';
break;
}
// write something to the newly created file
file << "This is file: " << ss.str() << "\n\nHello!\n";
if ( !file )
{
std::cerr << "Error: failed to write to file " << ss.str() << '\n';
break;
}
}
}
#include <iostream>
#include <fstream>
int main(void)
{
std::ofstream out; // you must call out.close() inside loop to be able to open another file for writting otherwise you'll get only the first one "a.txt"
std::string sFileName;
for(char c('a'); c < 'f'; c++)
{
sFileName = c;
sFileName += ".txt";
out.open(sFileName.c_str(), std::ios::out);
// std::ofstream out(sFileName.c_str(), std::ios::out); // here you are not obliged to call out.close() because the first out is not the very second and so on...
out.close(); // very important if you use the same ofstream to open another file
}
std::cout << std::endl;
return 0;
}