Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6.  
  7. void printHelp()
  8. {
  9. printf("Usage: fstring [ char ] [ amount ] Optional:[ outfile ]n"
  10. "Example: fstring A 100 out.txtn");
  11. exit(1);
  12. }
  13.  
  14. char *f_string(const char *s, int t)
  15. {
  16. int i; char *dst = malloc(t * strlen(s) + 1);
  17. for(i = 0; i < t; i++) {
  18. strcat(dst, s);
  19. }
  20. return dst;
  21. }
  22.  
  23.  
  24. int main(int argc, char *argv[])
  25. {
  26. char c; char *file;
  27.  
  28. while((c = getopt(argc, argv, "f:")) != -1)
  29. switch(c) {
  30. case 'f':
  31. file = optarg;
  32. break;
  33. default:
  34. printHelp();
  35. return 1;
  36. }
  37.  
  38. if(argc < 3) {
  39. printf("You need at least two arguments!n");
  40. return 1;
  41. }
  42.  
  43. char *res = f_string(argv[1], atoi(argv[2]));
  44. FILE *f = fopen(file, "w+");
  45. if(!f) {
  46. puts(res);
  47. exit(0);
  48. } else {
  49. fprintf(f, "%s", res);
  50. }
  51.  
  52. fclose(f);
  53. free(res);
  54. return 0;
  55. }
  56.  
  57. int main(int argc, char *argv[])
  58. {
  59. char line[80];
  60.  
  61. if(argc != 6) {
  62. fprintf(stderr, "You need to give 5 arguments!n");
  63. return 1;
  64. }
  65.  
  66. FILE *in;
  67. if(!(in = fopen("spooky.csv", "r"))) {
  68. fprintf(stderr, "File %s doesn't exist!", in);
  69. return 1;
  70. }
  71.  
  72. FILE *file1 = fopen(argv[2], "w");
  73. FILE *file2 = fopen(argv[4], "w");
  74. FILE *file3 = fopen(argv[5], "w");
  75.  
  76. while(fscanf(in, "%79s", line) == 1) {
  77. if(strstr(line, argv[1]))
  78. fprintf(file1, "%sn", line);
  79. else if (strstr(line, argv[3]))
  80. fprintf(file2, "%sn", line);
  81. else
  82. fprintf(file3, "%sn", line);
  83. }
  84.  
  85. fclose(file1);
  86. fclose(file2);
  87. fclose(file3);
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement