Recent Posts
None | 1 sec ago
Bash | 6 sec ago
None | 36 sec ago
PHP | 1 min ago
None | 1 min ago
PHP | 1 min ago
C++ | 1 min ago
None | 1 min ago
ActionScript 3 | 1 min ago
None | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 9th of Feb 2010 09:12:28 PM Download | Raw | Embed | Report
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int
  6. main(int argc, char **argv)
  7. {
  8.         if (3 != argc)
  9.         {
  10.                 fprintf(stderr, "%s <inputfile> <outputfile>.\n", argv[0]);
  11.                 return EXIT_FAILURE;
  12.         }
  13.  
  14.         // Try to open input file
  15.         FILE *fInput = fopen(argv[1], "r");
  16.         if (NULL == fInput)
  17.         {
  18.                 fprintf(stderr, "Could not open input file.\n");
  19.                 return EXIT_FAILURE;
  20.         }
  21.  
  22.         // Try to open output file
  23.         FILE *fOutput = fopen(argv[2], "w");
  24.         if (NULL == fInput)
  25.         {
  26.                 fprintf(stderr, "Could not open output file.\n");
  27.                 return EXIT_FAILURE;
  28.         }
  29.  
  30.         // Determine the data length
  31.         fseek(fInput, 0L, SEEK_END);
  32.         long fInput_size = ftell(fInput);
  33.         fseek(fInput, 0L, SEEK_SET);
  34.  
  35.         // Allocate memory
  36.         char *data = (char *)malloc(fInput_size);
  37.         if (NULL == data)
  38.         {
  39.                 fprintf(stderr, "Could not allocate enough memory.\n");
  40.                 return EXIT_FAILURE;
  41.         }
  42.  
  43.         // Read the file content
  44.         if (0 == fread(data, fInput_size, 1, fInput))
  45.         {
  46.                 fprintf(stderr, "Could not read entire file.\n");
  47.                 return EXIT_FAILURE;
  48.         }
  49.         fclose(fInput);
  50.  
  51.         // Remove the first line
  52.         char *p;
  53.         char *end = data + fInput_size;
  54.         for(p = strstr(data, "\n") + 1; p < (data + fInput_size); )
  55.         {
  56.                 // Determine the next row
  57.                 char *next_row = strstr(p, "\n");
  58.                 if (NULL == next_row)
  59.                 {
  60.                         next_row = end;
  61.                 }
  62.  
  63.                 // Skip the first 3 ";"
  64.                 unsigned int i;
  65.                 for(i = 0; i < 3; i++)
  66.                 {
  67.                         p = strstr(p, ";") + 1;
  68.                 }
  69.  
  70.                 // Work remaining fields
  71.                 for(i = 0; p < next_row; i++)
  72.                 {
  73.                         // Determine the next separator
  74.                         char *next_semi = strstr(p, ";");
  75.                         if (NULL == next_semi || next_semi > next_row)
  76.                         {
  77.                                 next_semi = next_row;
  78.                         }
  79.                         if (0 == strncmp("unclassified", p, 12))
  80.                         {
  81.                                 fprintf(fOutput, "0 ");
  82.                         }
  83.                         else if (0 == strncmp("0", p, 1))
  84.                         {
  85.                         }
  86.                         else if (0 == strncmp(";", p, 1))
  87.                         {
  88.                         }
  89.                         else
  90.                         {
  91.                                 fprintf(fOutput, "%u:", i);
  92.                                 fwrite(p, (next_semi - p), 1, fOutput);
  93.                                 fprintf(fOutput, " ");
  94.                         }
  95.  
  96.                         p = next_semi + 1;
  97.                 }
  98.                 fprintf(fOutput, "\n");
  99.                 p = next_row + 1;
  100.         }
  101.        
  102.         // Clean up
  103.         fclose(fOutput);
  104.         free(data);
  105.  
  106.         return EXIT_SUCCESS;
  107. }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: