By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Problem with fread function

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ma**********@tlen.pl (marcinka) wrote in
news:f4**************************@posting.google.c om:
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.

--
- Mark ->
--
Nov 13 '05 #2

P: n/a
On 8 Dec 2003 06:53:45 -0800, ma**********@tlen.pl (marcinka) wrote:
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>>
Nov 14 '05 #3

P: n/a
On 8 Dec 2003 06:53:45 -0800, ma**********@tlen.pl (marcinka) wrote in
comp.lang.c:
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
Nov 14 '05 #4

P: n/a
Mark A. Odell wrote:
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?

Nov 14 '05 #5

P: n/a
Grumble <in*****@kma.eu.org> wrote in
news:br**********@news-rocq.inria.fr:
Mark A. Odell wrote:
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?


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.

--
- Mark ->
--
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.