Note that there are some explanatory texts on larger screens.

plurals
  1. POTo copy files in binary mode,why it doesn't work when we read to and write from a character variable?
    primarykey
    data
    text
    <p>The following program is intended to make a copy of one <strong>.exe</strong> application file.But just one little thing determines whether it indeed gives me a proper copy of the intended file <strong>RealPlayer.exe</strong> or gives me a corrupted file.</p> <p>What I do is read from the source file in binary mode and write to the new copy in the same mode.For this I use a variable <code>ch</code>.But if <code>ch</code> is of type <code>char</code>, I get a corrupted file which has a size of few bytes while the original file is <strong>26MB</strong>.But if I change the type of <code>ch</code> to <code>int</code>, the program works fine and gives me the exact copy of <strong>RealPlayer.exe</strong> sized <strong>26MB</strong>.So let me ask two questions that arise from this premise.I would appreciate if you can answer both parts:</p> <p><strong>1)</strong> Why does using type <code>char</code> for <code>ch</code> mess things up while <code>int</code> type works?What is wrong with <code>char</code> type?After all, shouldn't it read byte by byte from the original file(as <code>char</code> is one byte itself) and write it byte by byte to the new copy file?After all isn't what the <code>int</code> type does,ie, read 4 bytes from original file and then write that to the copy file?Why the difference between the two?</p> <p><strong>2)</strong> Why is the file so small-sized compared to original file if we use <code>char</code> type for <code>ch</code>?Let's forget for a moment that the copied file is corrupt to begin with and focus on the size.Why is it that the size is so small if we copy character by character (or byte by byte), but is big(original size) when we copy "integer by integer" (or 4-bytes by 4-bytes)?</p> <p>I was suggested by a friend to simply stop asking questions and use <code>int</code> because it works while <code>char</code> doesn't!!.But I need to understand what's going on here as I see a serious lapse in my understanding in this matter.Your detailed answers are much sought.Thanks.</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; int main() { char ch; //This is the cause of problem //int ch; //This solves the problem FILE *fp,*tp; fp=fopen("D:\\RealPlayer.exe","rb"); tp=fopen("D:\\copy.exe","wb"); if(fp==NULL||tp==NULL) { printf("Error opening files"); exit(-1); } while((ch=getc(fp))!=EOF) putc(ch,tp); fclose(fp); fclose(tp); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload