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

时间:2016-09-25 11:13:37

标签: c++ file

我想用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??

 }
}

我对在循环中写的内容感到困惑。我该如何创建这些文件?

3 个答案:

答案 0 :(得分:3)

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();
}

答案 1 :(得分:2)

您可以使用std::stringstream撰写文件名,然后将其作为std::ofstream传递给std::string构造函数。

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

答案 2 :(得分:0)

#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对象,你必须关闭先前的文件才能打开下一个文件,否则它将无法尝试创建下一个文件。