I am writing a code to read file and output stuff for two files and combine them together... Don't know why they read good at the first file but wit the same code , it didn't show up for the second file
Please help
Thanks

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


int main() {
  FILE *file;
  int i;
  int j=0;
  char fileparam[16] ;
  char arraytotal [200];
  int start =0 ;
  int k = 0 ;
  

 
            if(sprintf(fileparam, "in0.dat",i) )
                {
                     file = fopen(fileparam,"r");
                    if(file==NULL)
                    {
        				 printf("Error: %s does not exist\n",fileparam);
                         
                    }
                    else         
                    {
            	       printf("%s was opened successfully.\n", fileparam);	      
            	      
            	       printf("Good\n");
                        for ( k = start; k <sizeof arraytotal; k++)
            	       {
                           arraytotal[k] = (char)fgetc(file);
                           printf("%c",arraytotal[k]);
                           start++;
                       } // end for
        
                    }
                
                } 
                  if(sprintf(fileparam, "in1.dat",i) )
                {
                     file = fopen(fileparam,"r");
                    if(file==NULL)
                    {
        				 printf("Error: %s does not exist\n",fileparam);
                         
                    }
                    else         
                    {
            	       printf("%s was opened successfully.\n", fileparam);	      
            	      
            	       printf("Good\n");
            	        for ( k = start; k <sizeof arraytotal; k++)
            	       {
                           arraytotal[k] = (char)fgetc(file);
                           printf("%c",arraytotal[k]);
                           start++;
                       } // end for
            	 
        
                    }
                
                } // end if 
               
            printf("Final Result is \n");
                   for ( k = 0; k <sizeof arraytotal; k++)
        	       {
                      
                       printf("%c",arraytotal[k]);
                      
                   } // good code for open file
        
  
// the rest of process related to the file are trim
	
 fclose(file);
 
   system("pause");
return 0;
  
}

You forgot to close the handle after reading the first file:

fclose(file);

Also, it is good programming practice to:

  1. Initialize all pointers to NULL (e.g. FILE *file = NULL;)
  2. Create a method for repeating code. You have copied the same code twice only to open two different files.

This is also incorrect:

k = start; k <sizeof arraytotal; k++

First off, the parentheses are missing (sizeof(arraytotal)).
Second, calling sizeof of an array will return the number of bytes allocated by that array, not the length of the array!

Nitpick...

First off, the parentheses are missing (sizeof(arraytotal)).

Although most programmers use sizeof(x) , it's perfectly acceptable to say sizeof x .

Ah! I thought it only works with parentheses.

If you're using the name of a type, parens are required, otherwise the operand is a unary expression and parens are optional:

sizeof(int) /* OK */
sizeof int  /* Error */
int x;

sizeof(x) /* OK */
sizeof x  /* OK */

Use perror() to get more details about why the file couldn't be opened:

file = fopen(fileparam, "r");

if (file == NULL) {
    fprintf("Error opening '%s' -- ", fileparam);
    perror(NULL);
} else {
    /* ... */
}

It's not always safe to assume that an fopen() failure is because the file doesn't exist.

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