I need to know can i make a for loop to place fopen function to open a set of files like i want file1.in file2.in file3.in .......filen.in so on depending on n value i give for num of files to be created ...
as i want to place set of content in each file.in after it is created and opened
..!!
Can someone help me out with this .. in C++??

>>Can someone help me out with this .. in C++??
Help? yes. c++ no. fopen() is a C function, not C++;

char *filenames[] = {"file1","file2","file3",NULL};

for(int i = 0; filenames[i] != NULL; i++)
{
    FILE* fp = fopen(filenames[i], "r");
    // do something

    // now close the file
    fclose(fp);
}

or else you can use an array of FILE* if you want to use them elsewhere in the program. or write a structure with elements as FILE* and filename and use an array of that structure.

>>Can someone help me out with this .. in C++??
Help? yes. c++ no. fopen() is a C function, not C++;

char *filenames[] = {"file1","file2","file3",NULL};

for(int i = 0; filenames[i] != NULL; i++)
{
    FILE* fp = fopen(filenames[i], "r");
    // do something

    // now close the file
    fclose(fp);
}

I tried doin the above as you said. I m jus getting one file with name filenames, nothing else. I need somoething like file1.dat file2.dat file3.dat and tilll file100.dat files.. !!

You can only open one file at a time -- C and C++ languages do not support opening multiple files with the same FILE pointer or c++ stream at the same time. So you have to open a file, do something, close the file, then repeat that process for each filename.

You can, however have an array of FILE pointers all open at the same time, but each one can only open one file. That's usually a pretty waste of program resources and not normally recommended except when there are only a couple of files that have to be open at the same time.

or else you can use an array of FILE* if you want to use them elsewhere in the program. or write a structure with elements as FILE* and filename and use an array of that structure.

can you tell me how can i do tht !! can u help me with dat small block of code !!!

You can only open one file at a time -- C and C++ languages do not support opening multiple files with the same FILE pointer or c++ stream. So you have to open a file, do something, close the file, then repeat that process for each filename.

yah i want to open a file file1.dat write content into it ..close and oen a new file with a name file2.dat write content n close it .. and continue this !!

yah i want to open a file file1.dat write content into it ..close and oen a new file with a name file2.dat write content n close it .. and continue this !!

I assumed you needed multiple files open at the same time, for above requirement AD has already given you the code. Go through it.

yah i want to open a file file1.dat write content into it ..close and oen a new file with a name file2.dat write content n close it .. and continue this !!

Great :) I already posted an example of exactly that algorithm. One problem may be how to get a list of the filenames. Do you want to create them at runtime, such as appending a number after a commen name? Or get a list of existing filenames ? Or something else ?

Great :) I already posted an example of exactly that algorithm. One problem may be how to get a list of the filenames. Do you want to create them at runtime, such as appending a number after a commen name? Or get a list of existing filenames ? Or something else ?

yah i saw that but when i tried to run it . i just get one file wid name filenames thats it . I dont get wht i want . i.e., file1.dat and some fprint statement to enter some content and close file2.dat and some more content n close and so on !!!

Post code because its a little hard for me to see what you wrote on your computer.

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

using namespace std;
main()
{  
  char *filenames[] = {"file1","file2","file3",NULL};
  //int n;  
  //cout<<" enter number of input files :";
  //cin>>n;
  for(int i =1;filenames[i] != NULL;i++){
  FILE *fp = fopen ( "filenames[i].dat","w");
  fprintf(fp,"Copy \"C:\\Program Files\\prog\\prog%d.IN\" prog.In\n",i);
  fclose(fp);
}
}

Actually i m gettin only 3 files i declared but i need some 100 files like tht.. I cant write somany at a time ... so can u suggest soemthing tht i can do .. to get tht !!

I think you should change your code as following:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

using namespace std;
main()
{ 
  // char *filenames[] = {"file1","file2","file3",NULL};
  int n; 
  char buf[256];
  FILE *fp = NULL;
  cout<<" enter number of input files :";
  cin>>n;
  for(int i =0;i < n;i++){
    sprintf(buf,"file%d.dat",i+1);
    fp = fopen ( buf,"w");
    if (fp == NULL) break;
    fprintf(fp,"Copy \"C:\\Program Files\\prog\\prog%d.IN\" prog.In\n",i+1);
    fclose(fp);
   }
}
Comments
Thanks for taking the time to use code tags :)

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

using namespace std;
main()
{
char *filenames[] = {"file1","file2","file3",NULL};
//int n;
//cout<<" enter number of input files :";
//cin>>n;
for(int i =1;filenames != NULL;i++){
FILE *fp = fopen ( "filenames.dat","w");
fprintf(fp,"Copy \"C:\\Program Files\\prog\\prog%d.IN\" prog.In\n",i);
fclose(fp);
}
}

I think you should change your code as following:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

using namespace std;
main()
{ 
  // char *filenames[] = {"file1","file2","file3",NULL};
  int n; 
  char buf[256];
  FILE *fp = NULL;
  cout<<" enter number of input files :";
  cin>>n;
  for(int i =0;i < n;i++){
    sprintf(buf,"file%d.dat",i+1);
    fp = fopen ( buf,"w");
    if (fp == NULL) break;
    fprintf(fp,"Copy \"C:\\Program Files\\prog\\prog%d.IN\" prog.In\n",i+1);
    fclose(fp);
   }
}

Thanks a lot .. I got it workin !!

This question has already been answered. Start a new discussion instead.