I am writing code to open file and read info from that file to open other files,
dont' know why it's only open the first file, not file after that
THanks

/* strtok example */
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    
    using namespace std;
    void remove ( char str[] ,int rem) ;
    int main ()
    {
         FILE *file;
      int i;
      int j=0;
    
      // char fileparam1[16] ;
      char arraytotal [15000];
      int start =0 ;
      int k = 0 ; // for the rest 
      int u = 0 ; //for final loop
      int t = 0; // for in.dat file 
      char fileparam[15000];
        char str[15000] ; 
            file = fopen("in.dat", "r");
        if(file==NULL)
        {
			 printf("Error: File does not exist\n");
             
        }
        else         
        {
	       printf("FILE WAS OPEN SUCCESSFULLY\n");	      
	      
	       printf("CONTENT OF FILE in.dat is \n");
            while(!feof(file))
	       {
               str[t] = (char)fgetc(file);
                 printf("%c",str[t]);
               t++;
           } // end while
                 char * pch;
      printf ("\nSplitting string \"%s\" into tokens:\n",str);
      pch = strtok (str," .dat"); 
      strcpy(fileparam,pch);
      while (pch != NULL)
      {
                strcpy(fileparam,pch);
                strcat(fileparam,".dat");
                printf ("\n%c\n",fileparam);
                pch = strtok (NULL, ".dat");
           
            } // end while
                         
                         int a= 1; 
            while(a=1)
            {            
           
                file = fopen(fileparam,"r");
             if(file==NULL)
            {
				 printf("Error: %s does not exist\n",fileparam);
                 a=0;
            }
            else         
            {
    	       printf("%s was opened successfully.\n", fileparam);	      
    	      
    	       printf("Good\n");
                while(!feof(file))
    	       {
                   arraytotal[k] = (char)fgetc(file);
                     printf("%c",arraytotal[k]);
                   k++;
               } // end while

            } // end else 
         fclose(file); // close file in0        
        }


    
    
    
    //Remove string character
    
     i=0;
     for( i=0;i <=3;i++)
     {
      if(isdigit((int)str[i]))
      { 
    
         remove ( str ,i)    ;    
            
      }
          
          }
                       
     
                  
      
     system("pause");
    
      return 0;
    }
    
     // remove rem th elements
     void remove ( char str[] ,int rem) 
     {
        int i;
        int len = strlen(str);
         rem = 0;
    
        /* remove rem'th char from word */
        for (i = rem; i <= len ; i++)
         str[i] = str[i + 1];
       // if(i < len) word[i] = '\0';
    
        //printf("%s\n", str);
    }

Where did you close the first file before opening the second file with the same FILE variable?

Edited 5 Years Ago by WaltP: n/a

This article has been dead for over six months. Start a new discussion instead.