Problem with fread function

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
 
M

Mark A. Odell

(e-mail address removed) (marcinka) wrote in

Hello
This is the program, that I wrote.

#include <stdio.h>
#define MAX 4096

typedef unsigned short int int16;

What's your typedef for short int or signed short int going to be?
int16 o[MAX];
unsigned char i[MAX];

FILE *in, *out, *inn;
int amount;

These should be defined *inside* main().
int main(int argc, char* argv[])
{
out = fopen("test.out","w");
in = fopen("test.txt","r");

No check for success? If you want to count bytes w/o EOL concerns, open in
binary mode.
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;

Uselesss assignments to in and inn.
getchar();
return 0;
}

No error at run-time, but errors in understanding do exist. Re-read
documention on fopen and fread and try again.
 
B

Barry Schwarz

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");

Why are you writing out non-character data in text mode?
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++)

Do you really have a C99 compiler or do you take advantage of a
non-standard extension to just reduce the number of people who might
be able to help you.
o[k] = i[k]+1000; // line #2
fwrite(o, sizeof(int16), amount, out);

Why don't you check for how much was actually written.
if(ferror (out))
printf ("Error during saving.\n");
fclose(out);
out = NULL;
inn = fopen("test.out","r");

Why are you trying to read non-character data in text mode?
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]);

You do realize that o[k] is not an int and is unsigned. It could be
promoted to unsigned int (implementation dependent) which is not
consistent with the %d conversion specifier.
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.

There are lots of errors. They are just not the kind that ferror will
report.



<<Remove the del for email>>
 
J

Jack Klein

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");
[SNIP]

inn = fopen("test.out","r");

Rule #0: Read the FAQ before posting. In this particular case,
http://www.eskimo.com/~scs/C-faq/q12.38.html might have turned on the
light bulb over your head.

Rule #1: Open files for binary input and output in binary mode.

Rule #2: Don't even forget rule #1.

Rule #3: If you ever do forget rule #1, refer to rule #2.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
G

Grumble

Mark said:
What's your typedef for short int or signed short int going to be?

Aren't short int and signed short int the same type?

Also, is it correct that int becomes optional once short has been
specified?
 
M

Mark A. Odell

Aren't short int and signed short int the same type?

Sure, but since int16 is typedef for *unsigned* short I'd like to see what
the OP typedefs one of the signed shorts to. E.g. sint16? Why not use
int16 for short int and uint16 for unsigned short int instead?
Also, is it correct that int becomes optional once short has been
specified?

Yes, just as it is after long.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fread/fwrite 2 18
Fibonacci 0
fread() 6
fread() 2
URGENT 1
An interesting thing about fread(). 6
Working with files 1
Adding adressing of IPv6 to program 1

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top