Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char* argv[])
  5. {
  6. if (argc < 2) {
  7. printf("Prea putine argumente!\n");
  8. return 0;
  9. }
  10. //deschid fisierul pentru citire
  11. FILE * fp = NULL;
  12. fp = fopen(argv[1],"r");
  13. if (!fp) {
  14. printf("Nu s-a putut deschide fisier!\n");
  15. return 0;
  16. }
  17.  
  18. //aflu dimensiunea
  19. fseek(fp, 0L, SEEK_END);
  20. int size;
  21. size = ftell(fp);
  22. rewind(fp);
  23.  
  24. //pun fisierul intr-un buffer
  25. char * all = NULL;
  26. all = malloc((size+5)*sizeof(char));
  27. if (all) {
  28. fread(all,1,size,fp);
  29. }
  30. //inchid fisierul pentru citire
  31. fclose(fp);
  32.  
  33. //inversez caracterele in buffer
  34. int i,j;
  35. for (i=0, j=size-2; i<j; ++i, --j) {
  36. char aux;
  37. aux = all[i];
  38. all[i] = all[j];
  39. all[j] = aux;
  40. }
  41.  
  42. //deschid fisier pentru scriere
  43. fp = fopen(argv[1],"w");
  44.  
  45. //scriu bufferul inversat peste fisier
  46. fputs(all,fp);
  47.  
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement