Formatted IO iostream/stdio

I am trying to read a large, random data file with mixed data types and formats. I can use the c++ fscanf to harvest the information, but it seems like it is having problems with the read. Doubles are not getting the right values. The numbers are in large scientific format. I can't imagine that this has not been done before, but I am having a hard time finding it. The columns are fixed, but the numbers can shift inside of them and are somtimes missing. I need to be able to read a fixed width of characters and convert them to a number and store them. If it is not a number, I need to return 0. The resulting stream needs to be pointed to the next section. Is there an equivalent to the ostream << setw(int) for the istream? If so how do I use it?

I have been trying to use fscanf functions with a ifstream, but it fails, due to an incomatibility. How to I convert an istream& to a FILE *?

Any help would be appreciated.

Thanks,
Xydqyv
[994 byte] By [Xydqyv] at [2007-11-18 13:38:37]
# 1 Re: Formatted IO iostream/stdio
Can you post a sample of the data that you are reading?
Can you post your code which reads the data?
Include as much of the routine as possible. From the file open
to the file close.
cvogt61457 at 2007-11-9 0:27:38 >
# 2 Re: Formatted IO iostream/stdio
Here is the code:

-----------
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
int main (){
FILE *cfIn, *cfOut;
ifstream in;
ofstream out;
long node=0, a=0, b=0;
double x=0,y=0,z=0;
char buffer[255];
in.open("test.data");
out.open("test.data.out");
in.close();
out.close();
while(!in.eof()){
in.getline(buffer,255);
}
if ((cfIn = fopen("test.data","r"))==NULL)
{ cout<< "Read not opened" <<endl<<flush; return(1);}
if ((cfOut = fopen("test.data.out","w"))==NULL)
{ cout<< "Write file not opened" <<endl<<flush; return(1);}
while(!feof(cfIn)){
fscanf(cfIn,"%d%d%d%E%E%E",&node,&a,&b,&x,&y,&z);
cout << "Read "<<node<< " "<<a<<" "<<b<<" "<<x<<" "<<y<<" "<<z<<" "<<&cfIn<<&(cfIn[1])<<endl<<flush;
fprintf(cfOut,"%8d|%8d|%8d|%16.9e|%16.9e|%16.9e\n",node,a,b,x,y,z);
}

return(0);
}

-----------
The sample input is something like this:

20165 0 0 8.54950813 1.319062213e-02 6.25854926
20166 0 0 8.53367443 2.940708302e-02 6.25480276
20167 0 0 8.74123004 3.641833084e-02 6.43496717
20168 0 0 8.75848943 3.239500379e-02 6.48667214
20169 0 0 8.88847866 0.172800315 6.89416161
20170 0 0 8.38000417 0.00000000 4.93453979
20171 0 0 8.40150208 -1.076254991e-12 4.93454502
20172 0 0 8.38000599 0.00000000 5.01637419
20173 0 0 8.38000781 0.00000000 5.09820859
20174 0 0 8.40148231 0.00000000 5.09821251

- I need the double precision for the numbers, but what is the format character for a double? I figured out that I am having a problem with data types on the read. I would sill like to be able to use scanf on iostream objects though if anyone knows how to manage that.

Thanks for the quick response ;-)
Xydqyv at 2007-11-9 0:28:38 >
# 3 Re: Formatted IO iostream/stdio
First thing: Without going through the code, remove the ".h" from
your STL header files.

#include <iostream>
#include <fstream>

The ".h" is depreciated and is no longer required.
This is for the STL header files.
cvogt61457 at 2007-11-9 0:29:43 >