fread perform differently from Window to Linux

Y

yexiangmo

I am facing a very weird problem of fread in VC6.0.

I have a binary file and use following testing code try to read in as
bytes
It works correctly in Linux and the final statement show the right
number
of bytes the program read in.

However, exact same code runs on windows read only few bytes and
thinks it reach the end of file. Do I miss something when I use fread
in windows?

Any idea can share?

Thanks.

Mark

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <float.h>
#include <math.h>

int main(int argc, char** argv)
{
int numBytes = 0;
char junk[1];
FILE *fpin = fopen( argv[1], "r+t");
if(fpin == NULL)
{
fprintf(stderr, "Cannot open file %s \n", argv[1]);
perror("Input file open failed\n");
return -1;
}
printf("file name is %s\n", argv[1]);
while(!feof(fpin))
{
if(fread(junk,1,1, fpin) == 1)
numBytes++;
}
printf("EOF reached after %d bytes read in!!!\n", numBytes);
return 0;
}
 
K

Karthik

I am facing a very weird problem of fread in VC6.0.

I have a binary file and use following testing code try to read in as
bytes
It works correctly in Linux and the final statement show the right
number
of bytes the program read in.

However, exact same code runs on windows read only few bytes and
thinks it reach the end of file. Do I miss something when I use fread
in windows?

Any idea can share?

Thanks.

Mark

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <float.h>
#include <math.h>

int main(int argc, char** argv)
{
int numBytes = 0;
char junk[1];
FILE *fpin = fopen( argv[1], "r+t");
You were mentioning about a binary file and you are opening in text mode
here.
FILE *fpin = fopen( argv[1], "rb");

should do the trick.
 
D

David Harmon

On 3 May 2004 08:28:57 -0700 in comp.lang.c++, (e-mail address removed)
([email protected]) wrote,
I have a binary file and use following testing code try to read in as
bytes ....
FILE *fpin = fopen( argv[1], "r+t");

No, actually you are reading it as a text file. The "t" in "r+t" most
explicitly says it is a text file, in addition to that being the
default. Open it as a binary file and you should be OK.
 
S

Sharad Kala

I am facing a very weird problem of fread in VC6.0.

I have a binary file and use following testing code try to read in as
bytes
It works correctly in Linux and the final statement show the right
number
of bytes the program read in.

As others have already pointed that you are not opening the file in binary
mode.
It does not work on Windows because Ctrl-Z(0x1A) is regarded as EOF in text
mode. Same is not the case on Unix (no EOF indicator) hence it works there. Your
byte stream might have contained this character.
Additionally CRLF character sequence is considered as newline, Unix considers
only LF as newline.

-Sharad
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top