--- In [email protected], Titi Anggono <[EMAIL PROTECTED]> wrote:
>
> Hi all,
>
> For example I want to open and write file in iteration
> using fopen. Here is that I've tried before.
>
> #define SIZE 10;
>
> ...
> int i;
> FILE *fp;
>
> for(i=0;i<SIZE;i++){
> if(i==0){
> fp=fopen("file1.dat","w");
> }
> else if(i==1){
> fp=fopen("file2.dat","w");
> }
> //so on
> /*Program and writing data into file is here*/
> fclose(fp);
> }
> ....
> ========================================
>
> I imagine if I have to set SIZE large number, I'll get
> bored just to write if(i==0) until if(i==50). Any
> suggestion how to overcome such problem using fopen ?
Titi,
that doesn't have anything to do with fopen(). What I understand what
you want is that you want to use fopen() with variable file names.
This can be achieved by making the file name variable, such as in this
piece of code:
#define SIZE 160
#define FNAME_SIZE (1<<12)
...
int i;
FILE *fp;
char File_Name [FNAME_SIZE];
for (int File_Index = 0; File_Index < SIZE; File_Index++)
{ sprintf( File_Name, "file%d.dat", File_Index + 1);
fp = fopen( File_Name, "w");
/*Program and writing data into file is here*/
fclose( fp);
}
....
Also I suggest that you choose a consistent naming and indentation
style according to your liking; modern editors usually make it very
simple to use long descriptive variable names with just two or three
keystrokes; long variable names won't make your code one byte longer
or one tick slower than short names; and such code will be far easier
to maintain for yourself in half a year or so.
If that's not what you need, please let us know.
Regards,
Nico
To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/c-prog/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/