There are multiple problems with your code, but:
The problem is the %s
at the beginning of the format string. %s
matches the complete line and thus contains all values.
Perhaps you can use %c
instead, if you are sure, there is only one char before the numbers.
Also notice that you passted a std::string
-Pointer to scanf
. This is invalid, since scanf
needs a char
buffer to store a string (%s
) which is not a good idea at all, since you don’t know the required length of the buffer.
This works for me:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
FILE *myfile;
myfile = fopen("tmp.txt", "r");
char type;
float dx;
float dy;
float intensity;
int nsat;
float rmsy;
float rmsx;
// The NULL-if should be here, but left out for shortness
while ( ! feof (myfile) )
{
fscanf(myfile,"%c %f %f %f %i %f %f",&type, &dx, &dy, &intensity, &nsat, &rmsx, &rmsy);
printf("F %f %f %f %i %f %f\n", dx, dy, intensity, nsat, rmsx, rmsy);
}
}
4
solved Getting Zeros When Reading A File Full of Numbers [closed]