0
#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.

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Salem
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.