prjbrook

C:\TinyC\MyFiles9May20\combine6.c

May 8th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. //https://stackoverflow.com/questions/16479006/can-i-use-fgetc-or-fputc-in-a-binary-file
  2. //Tue May 5 11:39:35 NZST 2020. Now want to chop 1802 bin file into lower and upper parts. 0-$400 and $4000-endoffile
  3. //Wed May 6 15:55:34 NZST 2020. Looking at command linelist of files.
  4. #include<stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. void ttest1(void);
  8. int fileIsReal(char* );
  9. int exists(const char *fname);
  10. int k;
  11. char str1[32] ;
  12. int main( int argc, char *argv[])
  13. { k=argc;
  14. strcpy(str1, argv[1]); //copies argv[1] into str1. May be useful.
  15.  
  16.  
  17. printf("Argc is %d.\n",argc);
  18. if( argc == 3 ) {
  19. printf("The first argument supplied is file %s. ", argv[1]);
  20. printf(" Its length is %d, ",strlen(argv[1]));
  21. char c1 = argv[1][strlen(argv[1])-1];
  22. printf("and the last character is '%c'. \n" , c1) ; //argv[1][strlen(argv[1])-1]);
  23. //if(c1!='x') system ("Notepad.exe");
  24. printf("The second argument supplied is file %s. ", argv[2]);
  25. printf(" Its length is %d, ",strlen(argv[2]));
  26. char c2 = argv[2][strlen(argv[2])-1];
  27. printf("and the last character is '%c'. \n" , c2) ; //argv[1][strlen(argv[1])-1]);
  28. }
  29. else if( argc > 3 ) {
  30. printf("Too many arguments supplied.\n");
  31. exit(4);
  32. }
  33. else {
  34. printf("Two arguments expected.\n");
  35. exit(5);
  36. }
  37. /*printf("Argc is %d, ",argc);
  38. printf("argv[1] is %s, ",argv[1]);
  39. printf("argv[2] is %s.\n ",argv[2]); */
  40. //FILE * fptr = fopen("BlinkNew3.ino.bin", "rb"); // open existingblink file for nano
  41. //FILE * fptr = fopen("CDP1802e91.ino.bin", "rb"); // open existingblink file for nano
  42. //FILE * fptr = fopen("CDP1802e93.ino.bin", "rb"); // open existingblink file for nano
  43. //printf("Result of exists() function for %s is %d\n",argv[1],exists(argv[1]));
  44. if(exists(argv[1] )) printf("File %s can be used. ",argv[1]);
  45. else {
  46. printf(" Can't find file %s.",argv[1]);
  47. exit(3);
  48. }
  49. if(exists(argv[2] )) printf(" File %s can be used.\n",argv[2]);
  50. else {
  51. printf(" Can't find file %s.",argv[2]);
  52. exit(3);
  53. }
  54. FILE * fptr = fopen(argv[1], "rb"); // open existingblink file for nano
  55. FILE * fptr1 = fopen( argv[2], "rb"); // open long file with 1802 code above $4000 and $0 to 1K
  56. //char buffer[10000] = {0}; // the pic is 6kb or so, so 10k bytes will hold it
  57. FILE * fptr2 = fopen("combinedFile0.bin", "wb"); // open a new binary file to be sent to mega
  58. FILE * fptr3 = fopen("low1802.bin", "wb"); // open a new binary file for vRAM[] insertion via terminal
  59.  
  60. unsigned long fileLen, fileLen1;
  61. unsigned long counter;
  62.  
  63. fseek(fptr, 0, SEEK_END);
  64. fileLen=ftell(fptr); // get the exact size of the blink file
  65. fseek(fptr, 0, SEEK_SET);
  66. printf("The size of argv[1], %s, is %d bytes. ",argv[1],fileLen);
  67.  
  68. fseek(fptr1, 0, SEEK_END);
  69. fileLen1=ftell(fptr1); // get the exact size of the blink file
  70. fseek(fptr1, 0, SEEK_SET);
  71. printf("The size of argv[2], %s, is %d bytes.\n ",argv[2],fileLen1);
  72.  
  73. for(counter=0; counter<fileLen; counter++)
  74. fputc(fgetc(fptr),fptr2); // read each byte of the arduino file. Put into combinedFile0
  75.  
  76. for(counter=fileLen; counter<0x4000; counter++) //fill in the gap between arduino file and 1802asm.bin
  77. fputc(0x00,fptr2);
  78.  
  79. fseek(fptr1,0x4000,SEEK_SET);
  80. for(counter=0x4000; counter<fileLen1; counter++)
  81. fputc(fgetc(fptr1),fptr2); //Put bytes from 1802.asm file that are above $4000 into combinedFile0
  82.  
  83. fseek(fptr1,0,SEEK_SET);
  84. for(counter=0; counter<0x400; counter++)
  85. fputc(fgetc(fptr1),fptr3); //Put bytes from 1802.asm file that are below $400 into low1802.bin .
  86.  
  87. fclose(fptr);
  88. fclose(fptr1);
  89. fclose(fptr2);
  90. fclose(fptr3);
  91. return 0;
  92. }
  93. void ttest1(void) {
  94. printf("The fttttirst argument supplied is %d", k);
  95. }
  96. /*int fileIsReal(char* filename) {
  97. int j;
  98. FILE * fptr = fopen(argv[1], "rb");
  99. }*/
  100. int exists(const char *fname)
  101. {
  102. FILE *file;
  103. if ((file = fopen(fname, "r")))
  104. {
  105. fclose(file);
  106. return 1;
  107. }
  108. return 0;
  109. }
Add Comment
Please, Sign In to add comment