Padding Numeric Outputs

I wrote a small program that generates CSV files for large data testing. The data is generated in a sequential matter.

For example the output in the txt file flows like this:

ID_1,A_1,B_1,C_1,D_1,E_1,F_1
ID_2,A_2,B_2,C_2,D_2,E_2,F_2
ID_3,A_3,B_3,C_3,D_3,E_3,F_3

My problem is that I want to add a certain number of 0's in front of the records depending on the number of numeric characters already generated.

For example for the first record it should be:

ID_00000001,A_00000001,B_00000001,C_00000001,D_00000001,E_00000001,F_00000001

But for record 356 it should be:

ID_00000356,A_00000356,B_00000356,C_00000356,D_00000356,E_00000356,F_00000356

So I need it to pad a certain number of 0's depending on the already present number 0's, what should I attempt to achieve this?
Here is the code that writes the output. Currently it will write-out 100,000 records based on the FOR loop.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  ofstream csvFile;
  csvFile.open ("csvDataBlock.txt");
  {
	cout << "Writing Output... \n";
	for ( int x = 0; x < 100000; x++ )
	{
		//Writes records based on variable in FOR loop, all field variables start at "1"

		//insert function for padding correction


		csvFile << "ID_" << fieldOne << ",A_" << fieldTwo << ",B_" << fieldThree << ",C_" << fieldFour << ",D_" << fieldFive << ",E_" << fieldSix << ",F_" << fieldSeven << "\n";
		fieldOne = fieldOne++;
		fieldTwo = fieldTwo++;
		fieldThree = fieldThree++;
		fieldFour = fieldFour++;
		fieldFive = fieldFive++;
		fieldSix = fieldSix++;
		fieldSeven = fieldSeven++;

	}
    csvFile.close();
Sorry, I haven't enough time. This code makes the followings:

000001
000002
000003
...


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
#include <iostream>
#include <sstream>
using namespace std;



int main()
{
	stringstream ss;
	string number;
	
	for ( int i = 0; i < 10; i++)
	{
		ss.clear();
		ss << i;
		ss >> number;
		
		if (number.length() < 6)
		{
			int dif = 6 - number.length();
			for (int j = 0; j < dif; j++) cout << "0";
		}
		cout << number << endl;
	}

	return 0;
}



Topic archived. No new replies allowed.