473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

fread perform differently from Window to Linux

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;
}
Jul 22 '05 #1
3 4109
ye*******@yahoo.com wrote:
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.

--
Karthik.
Humans please 'removeme_' for my real email.
Jul 22 '05 #2
On 3 May 2004 08:28:57 -0700 in comp.lang.c++, ye*******@yahoo.com
(ye*******@yahoo.com) 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.

Jul 22 '05 #3

<ye*******@yahoo.com> wrote in message
news:c1*************************@posting.google.co m...
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Patrice Kadionik | last post by:
Hi all, I want to make a brief comparison between read() and fread() (under a Linux OS). 1. Family read and Co: open, close, read, write, ioctl... 2. Family fread and Co: fopen, fclose,...
2
by: John | last post by:
I have a file open using fopen and am reaing using fread. Does this mean that the OS (linux) already buffers this file? What is the buffer size? Can I change it? Thanks, --j
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
4
by: annied | last post by:
Hi all, my program does the following: mbr = fopen(fullpath, "r"); if (!mbr) { fprintf(stderr, "VxVM ERROR V-5-3-000: Can't open device %s \n", ...
1
by: kirany82 | last post by:
Hi all, I am writign a program to get the process no(linux) into an integer. i am not able to get the process no into integer array...this is the program. please help me. #include <stdio.h>...
8
by: vippstar | last post by:
-- foo.c -- #include <stdio.h> int main(void) { struct { int a; int b; } foo; fread(&foo, sizeof foo, 1, stdin); return 0; } -- foo.c --
4
by: Giacomo | last post by:
Hello.. i'm using php on linux --version: PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies and...
17
by: yawnmoth | last post by:
http://www.frostjedi.com/terra/scripts/demo/this-alert.html http://www.frostjedi.com/terra/scripts/demo/this-alert2.html Why, when you click in the black box, do the alert boxes say different...
15
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.