Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- int main(int argc, char *argv[]){
- //Checking if the user put in correct number of arguments.
- if (argc != 3){
- printf("Invalid number of arguments.\n");
- exit(1);
- }
- FILE *fp1;
- FILE *fp2;
- fp1 = fopen(argv[1], "rb");
- //Checking if the file opened correctly
- if (fp1 == NULL){
- perror("fopen");
- exit(1);
- }
- char numbers[44];
- short num[2];
- //Reading file
- //arg1 = pointer to file where read stuff is stored
- //arg2 = Size of the element: in this case char (size=1)
- //arg3 = number of elements: here 44 elements coz 44 X 1 = 44 bytes
- //arg4 = file name to be read
- fread(numbers, sizeof(char), 44, fp1);
- fp2 = fopen(argv[2], "wb");
- fwrite(numbers, sizeof(char), 44, fp2);
- size_t input1 = fread(&num[0], sizeof(short), 1, fp1);
- size_t input2 =fread(&num[1], sizeof(short), 1, fp1);
- short new = (num[0] - num[1])/2;
- fwrite(&new, sizeof(short), 1, fp2);
- fwrite(&new, sizeof(short), 1, fp2);
- while(input1 != 0 && input2!=0){
- input1 =fread(&num[0], sizeof(short), 1, fp1);
- input2 =fread(&num[1], sizeof(short), 1, fp1);
- new = (num[0] - num[1])/2;
- fwrite(&new, sizeof(short), 1, fp2);
- fwrite(&new, sizeof(short), 1, fp2);
- }
- fclose(fp1);
- fclose(fp2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement