0x00sec_JINX

charnum.c

Jun 27th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6.  
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; even without the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. GNU General Public License for more details.
  11.  
  12. You should have received a copy of the GNU General Public License
  13. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14.  
  15. All code posted on my Pastebin is designed for Linux (Debian-based)
  16. compile with: gcc charnum.c -o charNum
  17.  
  18. A program to determine how many characters are in a file
  19. (such as .C, .py, .txt, etc...)
  20. Note: Does not include newline characters
  21.  
  22. Original creator: 0x00sec_JINX (https://0x00sec.org/users/0x00_jinx)
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <strings.h>
  27. #include <unistd.h>
  28.  
  29. int main(int argc, char *argv[]){
  30.  
  31.     if(argc != 2){
  32.  
  33.         printf("Usage: %s <filename>\n",argv[0]);
  34.         return 1;
  35.  
  36.     }
  37.  
  38.     FILE *fd;
  39.     char buff[2];
  40.     int charCount = 0;
  41.  
  42.     if((fd = fopen(argv[1],"r")) < 0){
  43.  
  44.         printf("ERROR Could not open file %s",argv[1]);
  45.         return 1;
  46.  
  47.     }
  48.  
  49.     while((fgets(buff,sizeof(buff),(FILE*)fd)) != NULL){
  50.  
  51.         if((buff[1] != '\n') && (buff[0] != '\n')){
  52.  
  53.             ++charCount;
  54.  
  55.         }
  56.  
  57.     }
  58.  
  59.     fclose(fd);
  60.  
  61.     printf("%d\n",charCount);
  62.  
  63.     return 0;
  64.  
  65. }
Add Comment
Please, Sign In to add comment