473,322 Members | 1,494 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,322 software developers and data experts.

Not reading in what was written out...

All, I'm hoping you can provide some help. I have opened a temporary
file, written to it, verified the correct amount was written, done
freopen() for the file to stdin, then try read the information back,
but 0 amount is being read in. I have done ferror() on the stream and
it is still 0, and freopen() != NULL.

Output from passing in 20 short values and writing them out as float
(istat is the return of fread() ):
fwrite: 10
istat: 0 ferror:0

Output from passing in 6000 short values and writing them out as float
(istat is the return of fread() ):
fwrite: 3000
istat: 2048 ferror:0
istat: 0 ferror:0
========Code========
#define FLOAT 0
#define INDIM 32768
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

main (argc,argv)
int argc;
char **argv;

{
double dfloatarray[INDIM];
float floatarray[INDIM];
int i,j,k,iop,jop,kop,nneg,abs,istat,iaccum,intype,oty pe;
int iostat,icheck;
int norm=0;
FILE *outplace;
char tempfile[] = ".dconvert_tempXXXXXX";
FILE *tempfile_fd;

/*.... Parse command line....*/

intype = FLOAT;

iaccum = 0;
if (( tempfile_fd = fdopen( mkstemp(tempfile) , "w+" )) == NULL){
fprintf(stderr,"Could not create temporary file\n");
}

while(feof(stdin) == 0){
if (intype == FLOAT) {
istat = fread(floatarray,sizeof(float),INDIM,stdin);
iaccum+=istat;
for (i=0; i<istat; i++){
dfloatarray[i] = (double) floatarray[i];
}
}

for (i=0;i<istat;i++){
floatarray[i] = (float) dfloatarray[i];
}

fprintf(stderr,"fwrite:
%d\n",fwrite(floatarray,sizeof(float),istat,tempfi le_fd) );
}

intype = FLOAT;
if(freopen(tempfile,"r+",stdin) == NULL) fprintf(stderr,"Didn't
reopen\n");

}

iaccum = 0;
while(1) {
if (intype == FLOAT) {

/*** This is where not reading the correct amount.... ***/

istat = fread(floatarray,4,INDIM,stdin);
fprintf(stderr,"istat: %d\tferror:%d\n",istat,ferror(stdin));
iaccum+=istat;
if (swapin == 1){
swapbytes(floatarray,istat,4);
}
for (i=0; i<istat; i++){
dfloatarray[i] = (double) floatarray[i];
}
}

/*....Continues on...*/


Any help is greatly appreciated! Thanks!

Josh

Jun 7 '06 #1
3 1934
jo**************@gmail.com wrote:
All, I'm hoping you can provide some help. I have opened a temporary
file, written to it, verified the correct amount was written,
Did you flush or close the temp file before attempting to read from it?
done freopen() for the file to stdin, then try read the information back,
but 0 amount is being read in. I have done ferror() on the stream and
it is still 0, and freopen() != NULL.

...
========Code========
#define FLOAT 0
#define INDIM 32768
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

main (argc,argv)
int argc;
char **argv;

{
double dfloatarray[INDIM];
float floatarray[INDIM];
That's potentially a large amount of automatic data. On many
systems, this will blow the hardware stack.
...
while(feof(stdin) == 0){


This is nearly always wrong. See the clc FAQ.

--
Peter

Jun 7 '06 #2

<jo**************@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
All, I'm hoping you can provide some help. I have opened a temporary
file, written to it, verified the correct amount was written, done
freopen() for the file to stdin, then try read the information back,
but 0 amount is being read in. I have done ferror() on the stream and
it is still 0, and freopen() != NULL.

Output from passing in 20 short values and writing them out as float
(istat is the return of fread() ):
fwrite: 10
istat: 0 ferror:0

Output from passing in 6000 short values and writing them out as float
(istat is the return of fread() ):
fwrite: 3000
istat: 2048 ferror:0
istat: 0 ferror:0
========Code========
#define FLOAT 0
#define INDIM 32768
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

main (argc,argv)
int argc;
char **argv;

{
double dfloatarray[INDIM];
float floatarray[INDIM];
int i,j,k,iop,jop,kop,nneg,abs,istat,iaccum,intype,oty pe;
int iostat,icheck;
int norm=0;
FILE *outplace;
char tempfile[] = ".dconvert_tempXXXXXX";
FILE *tempfile_fd;

/*.... Parse command line....*/

intype = FLOAT;

iaccum = 0;
if (( tempfile_fd = fdopen( mkstemp(tempfile) , "w+" )) == NULL){
fprintf(stderr,"Could not create temporary file\n");
}

while(feof(stdin) == 0){
if (intype == FLOAT) {
istat = fread(floatarray,sizeof(float),INDIM,stdin);
iaccum+=istat;
for (i=0; i<istat; i++){
dfloatarray[i] = (double) floatarray[i];
}
}

for (i=0;i<istat;i++){
floatarray[i] = (float) dfloatarray[i];
}

fprintf(stderr,"fwrite:
%d\n",fwrite(floatarray,sizeof(float),istat,tempfi le_fd) );
}

You haven't closed the file. What you wrote may still be in a buffer
somewhere, not actually in the file on disk.
intype = FLOAT;
if(freopen(tempfile,"r+",stdin) == NULL) fprintf(stderr,"Didn't
reopen\n");

}

iaccum = 0;
while(1) {
if (intype == FLOAT) {

/*** This is where not reading the correct amount.... ***/

istat = fread(floatarray,4,INDIM,stdin);
fprintf(stderr,"istat: %d\tferror:%d\n",istat,ferror(stdin));
iaccum+=istat;
if (swapin == 1){
swapbytes(floatarray,istat,4);
}
for (i=0; i<istat; i++){
dfloatarray[i] = (double) floatarray[i];
}
}

/*....Continues on...*/


Any help is greatly appreciated! Thanks!

Josh

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Jun 8 '06 #3
Awesome! Thanks so much for your help!

Jun 8 '06 #4

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

Similar topics

4
by: Yodai | last post by:
Hi all.. I'm trying to program an application for an emmbedded sistem where an external processor introduces data into my RAM. The thing is some if the data I have to pick up is written in...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
3
by: Sandeep | last post by:
How do I read file written in Java using C++. Is there a way of doing this in standard C++ or do I need to depend on technologies like soap for this kind of interop. -- Sandeep
4
by: ramyakrishnakumar | last post by:
Hi All, I am facing some problem with basic file operation... I have one xml file looks like <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <x:recording> <udf3>Gélin</udf3> ...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
8
by: boki_pfc | last post by:
Hi Everybody, I am looking for an advice on following: I have that "pleasure" of reading C++ codes that have been written by person(s) that have not attended the same C++ classes that I did or...
7
by: David Delony | last post by:
Since the best way to learn how to program well is to read other people's programs, which open source projects written in C stand out as examples of well-written code? -- There's no place...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
19
by: Hapa | last post by:
Does only reading (never writing) of a variable need thread synchronisation? Thanks for help? PS. Anybody knows a Visual C++ news group?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.