c++ - I am trying to use fread and fwrite to make a copy function but I keep getting strange output depending on the buffer size -
i trying write copy function takes in 3 arguments when run in command line. program run using name of program followed user specified buffer size , name of input file , name of output file.
e.g. >>copyf 1024 input.txt output.txt stored in argv[0] argv[3] respectively.
when run code buffer size of 1024 this:
this beginning of test file , want see how far makes in file before cutting stuff off.ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ
when run code buffer size of 32 this:
this beginning of test file , want see how far makes in file before cutting stuff off.tting stuff
here code:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string> int main(int argc, char*argv[]) { int end = 0; char buffer[65536]; //error checking if (argc != 4) { end = 1; printf("error-insufficient arguments (%d/4)\n", argc); } file*fp, *outfile; fp = fopen(argv[2], "r"); outfile = fopen(argv[3], "a"); if (fp == null) { end = 1; printf("error-input file fake\n"); } //no errors. proceed copy code int size = atoi(argv[1]); printf("buffer size is: %d bytes\n\n",size); if (end == 0) { printf("no errors detected\n\n"); while (!feof(fp)) { fread(&buffer, size, 1, fp); fwrite(&buffer, size, 1, outfile); (int = 0; < 65536; i++) { buffer[i] = null; } } } fclose(fp); fclose(outfile); clock_t endtime = clock(); float endtimesec = endtime; printf("runtime = %f\n\n", endtimesec / 1000); return 0; }
any suggestions on doing wrong appreciated. have feeling source of problem understanding of how fread , fwrite work in context.
there several problems code.
you doing little error handling (and not matters)
you opening files reading/writing text data instead of binary data, line breaks (if any) in file data possibly converted different format. true copy, don't want data change @ all.
you asking
fread()
read 1 elementsize
bytes in size. if input file size not multiple ofsize
, last element fail , ignored. account that, need askfread()
readsize
elements 1 byte in size.you ignoring return value of
fread()
, tells how bytes read. don't write more number of bytes output file.
try more instead:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string> int main(int argc, char*argv[]) { file *fp, *outfile; char buffer[65536]; int bufsize, numread; if (argc != 4) { printf("error-insufficient arguments (%d/4)\n", argc); return 0; } bufsize = atoi(argv[1]); if ((bufsize < 1) || (bufsize > sizeof(buffer)) { printf("error-invalid buffer size (%d)\n", bufsize); return 0; } fp = fopen(argv[2], "rb"); if (fp == null) { printf("error-input file not opened\n"); return 0; } outfile = fopen(argv[3], "wb"); if (outfile == null) { printf("error-output file not created\n"); fclose(fp); return 0; } //no errors. proceed copy code printf("buffer size is: %d bytes\n\n", bufsize); { numread = fread(buffer, 1, bufsize, fp); if (numread < 1) { if ((ferror(fp) != 0) && (feof(fp) == 0)) printf("error-reading input file\n"); break; } if (fwrite(buffer, numread, 1, outfile) != 1) { printf("error-writing output file\n"); break; } } while (1); fclose(fp); fclose(outfile); clock_t endtime = clock(); float endtimesec = endtime; printf("runtime = %f\n\n", endtimesec / 1000); return 0; }
Comments
Post a Comment