Newbie with problem reading 6-column floating point data. I copied an example and expanded for my use, but get this output in OutFileOne.txt:
0.000
0.000
0.00
0.000
0.000
0.00
Not getting correct values, nor correct # of values in columns. Any suggestions? Have tried explicit format (%5.2f) with no luck.

//EXAMPLE: 

#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>

FILE * inFileOne;		//CStdioFile	inFileOne;
FILE * outFileOne;		//CStdioFile	outFileOne;

const int MAX=8192;

int main(void) 

{
	cout << endl;
// Declare Variables 
const int MAX=8192;
float f, p, q, r, s, t; // field1, field2, field3, field4, field5, field6; 
float frq[MAX];
float DigI[MAX];
float DigQ[MAX];

	inFileOne = fopen ("InPutDat1.txt","r");
	outFileOne = fopen ("OutDat1.txt","w");
/* InPutDat.txt:
  1.01  2.02  3.03  5.99  6.99  7.88
   .80  3.13  3.23  4.23  4.23  5.23
*/
for (int i=0; i <2; i++) // MAX; i++) - test just 2 lines for now
	{ 
  	fscanf  (inFileOne, "%f%f%f%f%f%f", &f,&p,&q,&r,&s,&t);
		frq[i]=f; //Set variables for use to inputs
		DigI[i]=p;
		DigQ[i]=q;

	printf ("3 Vals Read: %5.3f %5.3 %5.3 \n", &f,&p,&q);
	printf ("2 Vals Read: %8.3f %8.3 \n", &r,&s);
	printf ("1 Val  Read: %7.2f \n", &t);

	fprintf (outFileOne, "%5.3f %5.3 %5.3 \n", &f,&p,&q);
	fprintf (outFileOne, "%8.3f %8.3 \n", &r,&s);
	fprintf (outFileOne, "%7.2f \n", &t);
	}

// Close all files 
fclose(inFileOne);
fclose(outFileOne);
/* Sampe format examples:
   printf ("Characters: %c %f \n", 'a', 65.);
   printf ("Decimals: %d %ld\n", 1977, 650000L);
   printf ("Preceding with blanks: %10d \n", 1977);
   printf ("Preceding with zeros: %010d \n", 1977);
   printf ("Some different radixes: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
   printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
   printf ("floats: %f %+.1e %E \n", 3.1416, 3.1416, 3.1416);
   printf ("Width trick: %*d \n", 5, 10);
   printf ("%s \n", "A string");  
*/
return 0; 
}

Recommended Answers

All 3 Replies

Watch more closely what you're doing with the printf's.

printf ("3 Vals Read: %5.3f %5.3f %5.3f \n", f, p, q);
	printf ("2 Vals Read: %8.3f %8.3f \n", r, s);
	printf ("1 Val  Read: %7.2f \n", t);

	fprintf (outFileOne, "%5.3f %5.3f %5.3f \n", f, p, q);
	fprintf (outFileOne, "%8.3f %8.3f \n",  r, s);
	fprintf (outFileOne, "%7.2f \n",  t);

Watch more closely what you're doing with the printf's.

printf ("3 Vals Read: %5.3f %5.3f %5.3f \n", f, p, q);
	printf ("2 Vals Read: %8.3f %8.3f \n", r, s);
	printf ("1 Val  Read: %7.2f \n", t);

	fprintf (outFileOne, "%5.3f %5.3f %5.3f \n", f, p, q);
	fprintf (outFileOne, "%8.3f %8.3f \n",  r, s);
	fprintf (outFileOne, "%7.2f \n",  t);

I made mods suggested, but still getting errors:
0.000 -0.000 1.#QO
0.000
-0.00
0.000 -0.000 1.#QO
0.000
-0.00
Appear not to be accessing input data correctly. Also, don't understand unusual non-number results (#Q0), and only one entry on 2nd line when 2 are expected.

I made mods suggested, but still getting errors:
0.000 -0.000 1.#QO
0.000
-0.00
0.000 -0.000 1.#QO
0.000
-0.00
Appear not to be accessing input data correctly. Also, don't understand unusual non-number results (#Q0), and only one entry on 2nd line when 2 are expected.

****** Scratch that. Didn't change &f to f on output lines. Appears to be working now. Thanks greatly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.