c++ - Getting Zeros When Reading A File Full of Numbers -


i trying read file has several hundred lines. each line looks (keep in mind these not actual numbers. sample of format.) r 111.1111 222.2222 123456 11 50.111 51.111

i tried reading file fscanf , printing out of values when print out values, 0 variables. have checked file , none of lines have value of 0 variables. writing in c++.

#include <fstream>  #include <iostream>  #include <string>  using namespace std;  int main(int argc, char** argv)  {   file *myfile;   myfile = fopen("tmp.txt", "r");    string type;   float dx;   float dy;   float intensity;   int nsat;   float rmsy;   float rmsx;    if (myfile == null) exit(1);    else     {       while ( ! feof (myfile) )        {       fscanf(myfile,"%s %f %f %f %i %f %f\n",&type, &dx, &dy, &intensity, &nsat, &rmsx, &rmsy);       printf("f %f %f %f %i %f %f\n", dx, dy, intensity, nsat, rmsx, rmsy);         }     } } 

there multiple problems code, but:

the problem %s @ beginning of format string. %s matches complete line , contains values.

perhaps can use %c instead, if sure, there 1 char before numbers.

also notice passted std::string-pointer scanf. invalid, since scanf needs char buffer store string (%s) not idea @ all, since don't know required length of buffer.

this works 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;    // null-if should here, left out 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);   } } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -