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

eof problems in read double data from a file

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.