473,698 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2927
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.program mer.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1724
by: stephenma7 | last post by:
Hi, everybody. I am new here. I have encountered these many problems for the last couple of days. I have Linux Fedora Core 3(Gnu G++ 3.4.2), Linux Fedora Core 2 (Gnu G++ 3.3.3), Red Hat 9 (Gnu G++ 3.2.2, this is dual processors). I have the same set of programs that I wrote and when I ran on the three machines, which have different amount of memories, processors' speeds, and settings. I got three different seg faults at different...
4
1858
by: pisscot | last post by:
Lind Apr 13, 6:32 am show options Newsgroups: comp.lang.c From: piss...@gmail.com (Lind) - Find messages by this author Date: 13 Apr 2005 06:32:15 -0700 Local: Wed,Apr 13 2005 6:32 am Subject: how to read it out using c++ Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse
19
2506
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190, 21820, 1730, 2640, 3450, 4870, 6126, 7876, 15644, 17817, 20294, 21902, 2070, 3025, 4333, 5854, 7805, 9231, 10597, 16047........................... soo onnnnnn
14
1847
by: Henk | last post by:
Hi Guys, (see actual code below) I wrote a little program that is supposed to read a file (input.txt) into memory (using a stack) and then reverse the file and display it to output. It works, but I still get some errors when i try to compile it: stack.c: In function 'reverseFile'
9
2194
by: Sheldon | last post by:
Good day Everyone, I am a still very new at learning C and I have thrown myself in the deep end. I started with a simple program and kept widening the scope. This has taught me many things about C and some, I must admit, have not really sunk in yet. Still, I push on. Now I am taken a library of C programs that were designed to read HDF files. I work on a Unix server and in Mandrake10. The program below is most likely broken but I cannot...
0
5783
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read 17640 every times., and i set byte read=new byte, i have the wav format paper, but i don't know how to read the data correctly into the byte array, and used for doing DFT.... here's my code, and i am little hurry...thanks for the helps first~
6
2301
by: Trev17 | last post by:
Hello, I am trying to write a program that reads in a file, and uses a 2d array to store the highest and lowest temp from each month. It also outputs the average high and the average low. The file that i am reading in is called homework2.txt and looks like this: -4 25 6 33 21 50 33 67 50 72 65 80 72 95 65 101 58 82
6
2938
by: trl10 | last post by:
Hi Everyone, I'm still trying to learn C++ and this is my final project. We have to read sales data from a file into an array, process data in a partially filled array via functions, pass an array as a paameter to a function. I have most f the cde wrote, except that I can't get my readData function to work. I can't figure out hwto get it to read from the file and into an array. Here is the partial code #include <iostream> #include <fstream> ...
2
5838
by: steve005 | last post by:
Hi, I am writing a binary file with matlab that consists of a couple hundred double values and then reading it in with c++. The problem arises in c++ when I try and read in the files. At a specific point it stops and wont read any more values. I use this code to open it and read it: if( (err = fopen_s( &fp,"C:/simfile1.tmg0" , "rb" )) != 0 ); fread(&coneAngle,sizeof(double),1,fp); After about twenty simmilar reads it fails, returns an...
0
8676
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8867
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.