Wednesday, 26 August 2015

C++ none white-space character counter.

paramer 1 path of your text file.
if no paramter is passed it will default to a path.
#include <cstdio>
 
int main(int n, char *c[])
{ 
 char *a = 0 ? "C:\\tests\\nonwhitespacecount\\Debug\\1.txt" : c[1];
 FILE*f = fopen(a,"rb");
 if(f)
 {
  printf("unable to open %s\n", a);
  return 0;
 }
 fseek(f,0,SEEK_END);
 int size= ftell(f);
 char*r = new char[size+1];
 fseek(f,0,SEEK_SET);
 fread(r,1,size,f);
 fclose(f);
 f = fopen("out.txt","wb");
 
 int nonwhitespaces = 0;
 forint i =0; i < size; i++)
 {
  switch(r[i])
  {
   case 32:
   case 9:
   case 0:
   case 13:
   case 10:
    break;
   default:
    nonwhitespaces++;
    fwrite(r+i,1,1,f);
    break;
  }  
 }
 fclose(f);
 printf("nonwhitespaces %d.", nonwhitespaces);
 return 0;
}

No comments: