Advertisement
robonx12

CSV fucking tool

May 26th, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX 10
  6.  
  7. const char deteminator = ';';
  8.  
  9. int ReadNextString(FILE *, char *, int);
  10.  
  11. int main(int argc, char **argv) {
  12.  
  13.     char *vzorek = argv[1];
  14.     char string[MAX];
  15.     int read_return;
  16.     int sloupec = 1;
  17.     int radek = 1;
  18.     FILE *file;
  19.  
  20.     if (argc == 2) {
  21.         file = stdin;
  22.     }
  23.     else if (argc == 3) {
  24.         if ((file = fopen(argv[2], "r")) == NULL) {
  25.             fprintf(stderr, "Error opening file '%s'", argv[2]);
  26.             exit(1);
  27.         }
  28.     }
  29.     else {
  30.         fprintf(stderr,"Arguments error");
  31.         exit(1);
  32.     }
  33.    
  34.     while(1) {
  35.         memset(string, '\0', sizeof(char)*10);
  36.         read_return = ReadNextString(file, string, MAX);
  37.         if (strstr(string, vzorek) != NULL) {
  38.             printf("%s sloupec: %d radek: %d\n", string, sloupec, radek);
  39.         }
  40.         if (read_return == 10) {
  41.             radek++;
  42.             sloupec = 1;
  43.         }
  44.         if (read_return == deteminator) {
  45.             sloupec ++;
  46.         }
  47.         if (read_return == -1) {
  48.             break;
  49.         }
  50.         if (read_return == -2) {
  51.             fprintf(stderr, "Too long word, row: %d column: %d", radek, sloupec);
  52.             exit(1);
  53.         }
  54.     }
  55.     return 0;
  56. }
  57.  
  58. int ReadNextString(FILE *file, char *string, int max) {
  59.     int c;
  60.     int counter = 0;
  61.     while (1) {
  62.         c = getc(file);
  63.         if (feof(file)) {
  64.             return -1;
  65.         }
  66.         if (c == 10) {
  67.             return c;
  68.         }
  69.         if (c == deteminator) {
  70.             return deteminator;
  71.         }
  72.         if (c != deteminator && counter >= max) {
  73.             return -2;
  74.         }
  75.         else {
  76.             string[counter] = c;
  77.             counter++;
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement