I want to open different file names, at first it work well on couple files, but it has to be in order, what if i open random file like fall01, fall05,srping09 etc
Thanks

#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std; 
#include <iostream>


int main() {
  FILE *file;
  int i;
  char fileparam[16];
  
  for(i =1; i<=10;i++)
  {
           if( sprintf (fileparam, "fall%d.dat", i) == true )          
            {
            file = fopen(fileparam,"r");
            }
           if(file==NULL)
            {
				 printf("Error: %s does not exist\n",fileparam);
                 printf("Error: can't open file.\n");system("pause");
            }
           else
            {
	       printf("%s was opened successfully.\n", fileparam);	      
	       printf("Good\n");
	       system("pause");
	       }
	   
  }// done for
// the rest of process related to the file are trim
	
 fclose(file);
return 0;

  }

you might try something like

sprintf(fileparam, "%s%02d.dat", season_var, number_var);
Member Avatar for MonsieurPointer

Don't forget to close your file handle!

{
fclose(file);
printf("%s was opened successfully.\n", fileparam);
printf("Good\n");
system("pause");
}

Also, it is good practice to initialize all pointers to NULL:

FILE *file = NULL;

Don't forget to close your file handle!

{
fclose(file);
printf("%s was opened successfully.\n", fileparam);
printf("Good\n");
system("pause");
}

Also, it is good practice to initialize all pointers to NULL:

FILE *file = NULL;

It's also good practice to explain why system("pause"); is a bad thing...

Member Avatar for MonsieurPointer

It's also good practice to explain why system("pause"); is a bad thing...

Since I never use it, I don't have anything to explain... :) I did, however, follow up on your link. The horrors!

what is season_var, number_var

when I read your original post and source, I thought maybe you wanted variables for both the season and the number instead of hard coding or looping. if that wasn't what you wanted/needed, then I apologize.

#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#include <iostream>

only need this one

#include <stdio.h>
if( sprintf (fileparam, "fall%d.dat", i) == true )

sprintf returns number of characters written(int) in the array 'fileparam' or a negative number if an error occurred not a Boolean.

Your code when tidied up, looks like this:

#include <stdio.h>
int main(void) {
    FILE *file=NULL;
    int i;
    char fileparam[16];
    for(i =1; i<=10;i++){
        if( sprintf (fileparam, "fall%d.dat", i)>0){// returns positive if success
            if((file = fopen(fileparam,"w"))==NULL)
                printf("Error while opening file %s\n",fileparam);
            else {
                printf("%s was opened successfully.\n", fileparam);
                fclose(file);//close only if the file was opened
            }//done else
        
        }//done if
    }// done for

// the rest of process related to the file are trim


return 0;

 }
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.