#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp=NULL;
short i,a[10]={16,7,45,13,14,14,8,10,34,41},b[10];
fp=fopen(“a.txt”,“wb”);
if (!feof(fp)) fwrite (a,2,10,fp);
fclose (fp);
fp=fopen (“a.txt”,“rb”);
fread (b,2,10,fp);
for (i=0;i<10;i++) printf ("%d\n",b[i]);
fclose(fp);
}
if i use short, the file is copied successfully. btw using int, half the file is copied. what’s the reason behind that?
Maybe the problem the problem is in constant 2
, fwrite (a,2,10,fp)
, try
fwrite (a, sizeof(short), 10, fp)
your code prints all number on my machine
c:\CF>g++-4 --version
g++-4 (GCC) 4.5.3
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
using 2 or sizeof(int) gives the correct output in my machine, too. my question was why int doesn’t work here?
paste not working code instead of working one, when you want help…