I’m unable to write the integer value 13 into a file using any of the file handling functions available on C including putw(), fprintf(), and fwrite().
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
int arr[]={10, 11, 12, 13, 14, 15};
FILE *fp;
if (fp = fopen("test", "w")) {
for(i=0; i<6; i++) {
putw(arr[i], fp);
}
}
fclose(fp);
fp = fopen("test", "r");
while ((i=getw(fp))!= EOF) {
printf("%d, ",i);
}
fclose(fp);
getch();
}
gives an output
10, 11, 12, 3584, 3840,
and a modified array
int arr[]={13, 11, 12, 13, 14, 15};
gives an output
2816, 3072, 0, 14, 15,
You can notice that the output is not as expected after encountering the value 13.
Can anyone help me with this problem?
Looks like CR/LF line ending processing. Open your files in binary mode by using the “b” mode modifier, i.e.: