/* Link to the sample program http://www.java2s.com/Code/C/File/fscanffprintfexample.htm
*/
#include <stdio.h>
#include <io.h>
#include <stdlib.h>

int main(void)
{
  FILE* fin;
  FILE* fout; 
      
    if((fout=fopen("OUT2.txt","w")) == NULL) {           /*test=file name, r = read mode, which reads from the file*/
    printf("Cannot open file.\n");
    exit(1);
       }
       
    if((fin=fopen("DATA2.TXT","r")) == NULL) {           /*test=file name, r = read mode, which reads from the file*/
    printf("Cannot open file.\n");
    exit(1);
       } 
   
   int total=0;  
       
  int a;
  for(a=0;a<5;a++)
  {
     int b;
     char table[255];
     fgets(table, 255, fin); 
    int s1=0;
   for(b=0;b<255 && table[b]!=NULL; b++)
    {
     switch(table[b])
     {
         case '+':{ total++; break;}
         case '-':{ total--; break;}
         default:{printf("OMFGWTF"); break;}
     }
     
     if(total<0)
     {
                fprintf(fout,"OH NOES!\n");
                total=0;
                s1=1;
                break;   
     }       
 /*   fscanf(fin,"%c",&table[b]);  */
     /*  printf("%c", table[b]);*/
    }
   if(total>=0 && s1==0)
               fprintf(fout,"%d\n",total);
                
        
    
    
    
  
/*    printf("\n");*/



    }   

 

 /*   system("pause");*/
 
   fclose(fin);  
   fclose(fout); /*u need to close the file after opening it*/
  return 0;
}

/*So, fscanf just works like scanf, the only difference is u need to add the pointer b4 the statements, and the best way to read and write from files @ same time is to name 2 pointers like "fr" and "fw" 
if the file is stored in a specific location, do use like "C:/file/is/here/filename.we" */

