ketan18710

Untitled

Feb 16th, 2017
150
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. { int n=0;
  5. // ensure proper usage
  6. if (argc != 2)
  7. {
  8. fprintf(stderr, "Usage: ./copy infile outfile\n");
  9. return 1;
  10. }
  11. char *infile = argv[1];
  12. // open input file
  13. FILE *inptr = fopen(infile, "r");
  14. if (inptr == NULL)
  15. {
  16. fprintf(stderr, "Could not open %s.\n", infile);
  17. return 2;
  18. }
  19. int *buffer = malloc(512);
  20. if (fread(buffer,512,1,inptr)==512)
  21. {
  22. for(int i=0;i<512;)
  23. {
  24. if(buffer[i]==0xff&&buffer[i+1]==0xd8&&buffer[i+2]==0xff&&(buffer[i] & 0xf0)==0xe0)
  25. { char *a=NULL;
  26. sprintf(a,"%03i.jpg",n);
  27. n++;
  28. FILE *img = fopen(a,"w");
  29. for(int j=0;j<4;j++)
  30. {
  31. fwrite(buffer,1,1,img);
  32. i +=4;
  33. }
  34. while(!(buffer[i]==0xff&&buffer[i+1]==0xd8&&buffer[i+2]==0xff&&(buffer[i] & 0xf0)==0xe0))
  35. {
  36. fwrite(buffer,1,1,img);
  37. i++;
  38. }
  39. if(buffer[i]==0xff&&buffer[i+1]==0xd8&&buffer[i+2]==0xff&&(buffer[i] & 0xf0)==0xe0)
  40. {
  41. fclose(img);
  42. }
  43.  
  44. }
  45. }
  46. }
  47. fclose(inptr);
  48. }
RAW Paste Data