在循环中创建多个文本文件

create multiple text files inside a loop

本文关键字:文本 文件 创建 循环      更新时间:2023-10-16

我想用C++创建一些文本文件。例如:我将运行从1到5的循环,并创建以下文件:

1.txt
2.txt
3.txt
4.txt
5.txt

有可能吗?我做了一个示例代码:

#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??
   //...
}

如果sprintf对您的情况来说太简单,您可以使用它;


既然您标记了c++,我认为fstream string就是要使用的东西。

一个简单的c++示例

#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
   }
}

如果你仍然没有to_string(你没有c++11,或者你的编译器没有(,你现在可以使用这个简单的版本。(最好把这个放在你自己的namespace中(

#include <string>
#include <sstream>
std::string to_string(int i){
   std::stringstream s;
   s << i;
   return s.str();
}

在将文件名作为std::string传递给std::ofstream构造函数之前,可以使用std::stringstream来编写文件名。

#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() << "nnHello!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;
}

***要想在打开多个文件时使用一个ostream对象,必须关闭前一个文件才能打开下一个文件,否则它无法创建下一个。