Advertisement
Guest User

Untitled

a guest
May 21st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int max(int a, int b) { return (a > b) ? a : b; }
  8.  
  9. int main(int argc, char *argv[]) {
  10. FILE *tmpf = tmpfile();
  11. FILE *infile = fopen(argv[1], "r");
  12. string str = "";
  13. int max_len = 0;
  14. while (true) {
  15. char c = fgetc(infile);
  16. if (feof(infile)) break;
  17. fputc((int)c, tmpf);
  18. if (c != ' ') str += c;
  19. else {
  20. max_len = max(max_len, (int)str.length());
  21. str = "";
  22. }
  23. }
  24. fclose(infile);
  25. FILE *file = fopen(argv[1], "w");
  26. rewind(tmpf);
  27. str = "";
  28. int i = 0;
  29. while (true) {
  30. char c = fgetc(tmpf);
  31. if (feof(tmpf)) break;
  32. if (c != ' ') str += c;
  33. else {
  34. i++;
  35. int len = (int)str.length();
  36. for (int j = 0; j <= max_len + 1; j++) {
  37. if (j < len) fputc((int)str[j], file);
  38. else fputc((int)' ', file);
  39. }
  40. if (i % atoi(argv[2]) == 0) fputc((int)'\n', file);
  41. str = "";
  42. }
  43. }
  44. fclose(file);
  45. fclose(tmpf);
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement