468,456 Members | 1,840 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

eof problems in read double data from a file

When I write 'double' data to the file, it seems working fine, but when
I read it later, it returns a eof when it encounters a particular
number even the file is not at the end yet. However, while I write a
different set of data, there is no problem to read it.
Attached is the file: try change the #define RANGE 1000 into 10000,
the code works when the number is 1000, and does not work when the
number is 10000.
// trw.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#define NUM 2000
#define RANGE 1000
short initWrite (char *fileStr) {
FILE *fp;
//srand( (unsigned)time( NULL ) );

fp = fopen(fileStr, "w");
if (!fp) {
return -2;
}

size_t i = 0;

double t[NUM];

for (i=0; i<NUM; i++) {
if (i == 345)
t[i] = 0;
else
t[i] = rand () * RANGE/RAND_MAX;
}
i = fwrite (t, sizeof(double), NUM, fp);
fflush (fp);
fclose (fp);
return i;
}

short initRead (char *fileStr) {
FILE *fp;
//srand( (unsigned)time( NULL ) );

fp = fopen(fileStr, "r");
if (!fp) {
return -2;
}

double t[NUM];
fseek (fp, 0, SEEK_SET);
size_t i = fread (t, sizeof(double), NUM, fp);

if (feof (fp)) {
fprintf (stderr, "End of reading file\n");
}
else if (ferror(fp))
perror ("What;s wrong");

fclose (fp);
return !(i==NUM);
}
int _tmain(int argc, _TCHAR* argv[])
{

char *str = "D:/test.dat";

initWrite (str);
if (!initRead (str))
printf ("Good\n");
else
printf ("Something wrong\n");

/* for (int i=0; i<10000; i++) {
double f = updateFile (str);
if (!(i%10))
printf ("data is %5.2f\n", f);
}
*/
return 0;
}

Oct 2 '06 #1
3 2663
ak*******@gmail.com said:
// trw.cpp : Defines the entry point for the console application.
Try comp.lang.c++
#include "stdafx.h"
But wait! Try comp.os.ms-windows.programmer.win32 instead.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 2 '06 #2
ak*******@gmail.com wrote:
When I write 'double' data to the file, it seems working fine, but when
I read it later, it returns a eof when it encounters a particular
number even the file is not at the end yet. However, while I write a
different set of data, there is no problem to read it.
Attached is the file: try change the #define RANGE 1000 into 10000,
the code works when the number is 1000, and does not work when the
number is 10000.
// trw.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#define NUM 2000
#define RANGE 1000
short initWrite (char *fileStr) {
FILE *fp;
//srand( (unsigned)time( NULL ) );

fp = fopen(fileStr, "w");
if (!fp) {
return -2;
}

size_t i = 0;

double t[NUM];

for (i=0; i<NUM; i++) {
if (i == 345)
t[i] = 0;
else
t[i] = rand () * RANGE/RAND_MAX;
}
i = fwrite (t, sizeof(double), NUM, fp);
fflush (fp);
fclose (fp);
return i;
}

short initRead (char *fileStr) {
FILE *fp;
//srand( (unsigned)time( NULL ) );

fp = fopen(fileStr, "r");
if (!fp) {
return -2;
}

double t[NUM];
fseek (fp, 0, SEEK_SET);
size_t i = fread (t, sizeof(double), NUM, fp);

if (feof (fp)) {
fprintf (stderr, "End of reading file\n");
}
else if (ferror(fp))
perror ("What;s wrong");

fclose (fp);
return !(i==NUM);
}
int _tmain(int argc, _TCHAR* argv[])
{

char *str = "D:/test.dat";

initWrite (str);
if (!initRead (str))
printf ("Good\n");
else
printf ("Something wrong\n");

/* for (int i=0; i<10000; i++) {
double f = updateFile (str);
if (!(i%10))
printf ("data is %5.2f\n", f);
}
*/
return 0;
}
Open your files with "rb" and not "r". This could be the source of
your problems. "r" is for text files, and every \n character will be
translated into \r\n when writing, and a sequence of \r\n will become
\n when reading. With "rb" you avoid any translation.

Oct 2 '06 #3
jacob navia wrote:
Open your files with "rb" and not "r". This could be the source of
your problems. "r" is for text files, and every \n character will be
translated into \r\n when writing, and a sequence of \r\n will become
\n when reading. With "rb" you avoid any translation.
I forgot: for writing open them with "wb" and not "w".

Oct 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by stephenma7 | last post: by
4 posts views Thread by pisscot | last post: by
19 posts views Thread by ranjeet | last post: by
14 posts views Thread by Henk | last post: by
9 posts views Thread by Sheldon | last post: by
6 posts views Thread by Trev17 | last post: by
2 posts views Thread by steve005 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.