creating sequence of numbered files

I want to create a sequence of files like 001.dat, 002.dat and so on, so I try
1
2
3
4
5
6
7
for(int j = 0; j < 100; j++ )
{
	ostringstream sfname;
	sfname << setw(3) << i << ".dat";
	ofstream fout(sfname.str().c_str(), ios::out);
	fout.close();
}

the basic problem is that setw and fillchar do not seem to work with string streams, at least in vc2005/2008 compilers. is it non-standard? what is the proper way doing it? Thank you.
Well, I don't see you using setfill (I assume you meant that as opposed to fillchar) in your example code.
It will work regardless of stream type.
yes it is setfill, sorry, I just threw it away, to keep only one troublemaker in the code
1
2
3
4
5
6
7
for(int j = 0; j < 100; j++ )
{
	ostringstream sfname;
	sfname << setw(3) << setfill('0') << i << ".dat"; ..
	ofstream fout(sfname.str().c_str(), ios::out);
	fout.close();
}

compiling that results in
1
2
error C3861: 'setw': identifier not found
error C3861: 'setfill': identifier not found
1
2
#include <iomanip>
#include <iostream> 

:-)
Here's a program that does the same thing but not using stringstreams

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Step 1: Include libraries
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Step 2: Define used variables
ofstream f1;
ifstream f2;
string path;

int main()
{
        // Step 3: Use a mid-file to keep the addresses of all the files
	f1.open("log.txt");
	for (int x=1; x<=100; x++) {
		if (x<10) 
			f1 << "00";
		else if (x<100) 
			f1 << "0";
		
		f1 << x << ".dat\n";
	}
	f1.close();

	// Step 4: Create the files
	f2.open("log.txt");
	for (int x=1; x<=100; x++) {
		getline(f2, path);
		
		f1.open(path.c_str());
		f1.close();
	}
	f2.close();

	// Step 5: Delete the mid-file
	remove("log.txt");

	// Step 6: Inform the user that it's operation has been completed
        cout << "Done!\n";

	// Step 7: Close the program
	return 0;
}


Simple and efficient! (sorts of simple)
Last edited on
What?!

Lines 17-22 == Line 4.
All the extra file space == unnecessary.
*Stares in disbelief*
I... I'm sorry I...
I want to use Cpt. Picard here, but I... I'm sorry, this is just too much.
I can at least still comfort myself with the fact that I'll probably never have to work with you, CManowar.
Last edited on
Duoas
 
#include <iomanip> 

is the line I was missing. Thank you!
Last edited on
Topic archived. No new replies allowed.