M
marcinka
Hello
This is the program, that I wrote.
#include <stdio.h>
#define MAX 4096
typedef unsigned short int int16;
int16 o[MAX];
unsigned char i[MAX];
FILE *in, *out, *inn;
int amount;
int main(int argc, char* argv[])
{
out = fopen("test.out","w");
in = fopen("test.txt","r");
amount = fread(i, sizeof(unsigned char), MAX, in); // line #1
if(ferror (in))
printf ("Error during saving.\n");
for(int k=0; k<amount; k++)
o[k] = i[k]+1000; // line #2
fwrite(o, sizeof(int16), amount, out);
if(ferror (out))
printf ("Error during saving.\n");
fclose(out);
out = NULL;
inn = fopen("test.out","r");
amount = fread(o, sizeof(int16), MAX, inn); // line #3
if (ferror (inn))
printf ("Error during reading\n");
for(int k=0; k<amount; k++)
printf(" %d ",o[k]);
fclose(in);
fclose(inn);
in = NULL;
inn = NULL;
getchar();
return 0;
}
In my case file test.txt has 1542 elements and that is the value of
<amount> after line #1, but after line #3 it is only 81. It should be
the same value. The program works good only when I change my type
int16 to int or change line #2 to o[k]= i[k];. Every execution of
ferror function gives 0 value, so there is no error. Thanks for all
the answers.
Mariusz
This is the program, that I wrote.
#include <stdio.h>
#define MAX 4096
typedef unsigned short int int16;
int16 o[MAX];
unsigned char i[MAX];
FILE *in, *out, *inn;
int amount;
int main(int argc, char* argv[])
{
out = fopen("test.out","w");
in = fopen("test.txt","r");
amount = fread(i, sizeof(unsigned char), MAX, in); // line #1
if(ferror (in))
printf ("Error during saving.\n");
for(int k=0; k<amount; k++)
o[k] = i[k]+1000; // line #2
fwrite(o, sizeof(int16), amount, out);
if(ferror (out))
printf ("Error during saving.\n");
fclose(out);
out = NULL;
inn = fopen("test.out","r");
amount = fread(o, sizeof(int16), MAX, inn); // line #3
if (ferror (inn))
printf ("Error during reading\n");
for(int k=0; k<amount; k++)
printf(" %d ",o[k]);
fclose(in);
fclose(inn);
in = NULL;
inn = NULL;
getchar();
return 0;
}
In my case file test.txt has 1542 elements and that is the value of
<amount> after line #1, but after line #3 it is only 81. It should be
the same value. The program works good only when I change my type
int16 to int or change line #2 to o[k]= i[k];. Every execution of
ferror function gives 0 value, so there is no error. Thanks for all
the answers.
Mariusz