(Corrupted on top, original on bottom) I need a little help with this. I'm going outside my comfort zone (I have never worked with C/C++) here and working with a DOS 16Bit Compiler to produce an exe that will run in native DOS. I need to be able to open another exe file for editing as binary and write data to a couple specific locations and save the edited file. I can do all this, but what is happening is other parts of the file are becoming corrupted. I accurately write my data, and its of the correct length, but the file corrupts. This project requires DOS, and cannot be run from within Windows. Here is my CPP Code: #include <stdio.h> #include <string.h> int main(void) { FILE *key; key=fopen ("Testfile.exe","r+b"); char test1[100]; char test2[100]; printf("Test data to input:"); fgets(test1, sizeof test1, stdin); //printf("Your input: %s", test1); printf("Second test data to input:"); fgets(test2, sizeof test2, stdin); //printf("Your input: %s", test2); //Test1 24524 Test2 24583 and 24890 fseek (key,24523,SEEK_SET); //file offset location to begin write fwrite (test1,1,sizeof(test1),key); fseek (key,24582,SEEK_SET); //file offset location to begin write fwrite (test2,1,sizeof(test2),key); fseek (key,24889,SEEK_SET); //file offset location to begin write fwrite (test2,1,sizeof(test2),key); fclose(key); printf ("Finished"); return(0); } Any suggestions? Does anything jump out and shout "Hey, hes doing this all wrong!"? Im using Open Watcom Compiler with DOS 16 Bit Target. My exe runs just fine, but again, Testfile.exe gets corrupted.
For others looking to do the same, here is how I worked around it Code: #include<stdio.h> #include <string.h> int main(void) { FILE *key; key=fopen ("test.exe","r+b"); char test1[10]; char test2[32]; printf("Input Test1 data:"); scanf ("%10s",test1); //only read 10 Chars printf("Input test2 data:"); scanf ("%32s",test2); //only read 32 Chars fseek (key,24523,SEEK_SET); //file offset location to begin write fputs (test1,key); fseek (key,24582,SEEK_SET); //file offset location to begin write fputs (test2,key); fseek (key,24889,SEEK_SET); //file offset location to begin write fputs (test2,key); fclose(key); printf ("Finished"); return(0); }
sorry for the old thread, but mybe somebody is interested in the solution sizeof() return the size of the allocated memory (in your case 100)!! but you need the size of the string you must use strlen() note: in case your compiler allocate memory and write Null to the buffer -> all is ok in case your compiler allocate only memory you must fill the memory with Null !!!! char test1[100]; char test2[100]; memset (test1,0,sizeof(test1)); memset (test2,0,sizeof(test2)); Code: #include <stdio.h> #include <string.h> int main(void) { FILE *key; key=fopen ("Testfile.exe","r+b"); char test1[100]; char test2[100]; memset (test1,0,sizeof(test1)); // more robust (depend on compiler) memset (test2,0,sizeof(test2)); // more robust (depend on compiler) printf("Test data to input:"); fgets(test1, sizeof test1, stdin); //printf("Your input: %s", test1); printf("Second test data to input:"); fgets(test2, sizeof test2, stdin); //printf("Your input: %s", test2); //Test1 24524 Test2 24583 and 24890 fseek (key,24523,SEEK_SET); //file offset location to begin write //fwrite (test1,1,sizeof(test1),key); fwrite (test1,1,strlen(test1),key); fseek (key,24582,SEEK_SET); //file offset location to begin write //fwrite (test2,1,sizeof(test2),key); fwrite (test2,1,strlen(test2),key); fseek (key,24889,SEEK_SET); //file offset location to begin write //fwrite (test2,1,sizeof(test2),key); fwrite (test2,1,strlen(test2),key); fclose(key); printf ("Finished"); return(0); }