#include<stdio.h>
#include<conio.h>
#include<fstream.h>
void file()
{
     for(int i=0;i<30;i++)
     {
     char a[10]="2-2aa.pak";
     a[4]=a[4]+i%26;
     a[3]=a[3]+((i/26)%26);
     fstream file;
     file.open(a,ios::in |ios::out);
     file<<"anshulgarg";
     file.close();
     }
}

void main()
     {
     file();
     }

it creates 30 files with new names in bin but when i give relative path ex
"\\p\\2-2aa.pak" and adjust a[8],a[9] appropriately it creates only one file instead of thirty please tell me how to use relative path.
Aim is to create 30 new files with diff names
i want this code to work relative path as well.

main returns an int (see the avatar)

You could do

for ( i = 0 ; i < 30 ; i++ ) {
    sprintf( a, "file%02d.txt", i );
    fstream file;
    file.open(a,ios::in |ios::out);
    file<<"anshulgarg";
    file.close();
}

Which will generate file00.txt to file29.txt

This reacts much better to you changing the path, since you no longer rely on direct access to things like a[9] to change specific characters.

I've used sprintf(), but the same idea can be used with C++ strings as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.