#include #include #include int test() { const std::string filename("data.txt"); std::ifstream fin(filename.c_str()); if (!fin.good()) { std::cerr << "Cannot find file " << filename << "!" << std::endl; return 1; } Bool_t haveTrack=kFALSE; Bool_t haveWeight=kFALSE; Float_t en, wei; for (;;) { std::string tag; fin >> tag; if (fin.good()) if (tag=="trackin") { fin >> en >> en >> en >> en; haveTrack=kTRUE; } else if (tag=="weight") { fin >> wei >> wei; haveWeight=kTRUE; } else { std::cerr << "Unknown tag \"" << tag << "\"!" << std::endl; return 3; } else if (fin.eof()) break; else { std::cerr << "Cannot read file " << filename << "!" << std::endl; return 2; } if (haveTrack && haveWeight) { std::cout << "log10 of product: "; if (en*wei>0) std::cout << log10(en*wei) << std::endl; else std::cout << "ERROR - product " << en*wei << "<0!" << std::endl; haveTrack=haveWeight=kFALSE; } } return 0; } /* // you didn't open a file FILE* input_file=fopen("data.txt", "r"); char line[50] = "trackin"; char line2[50] = "weight"; float en_in, f4, en, wei, w2, w3; while(!feof(input_file)){ // you are reading a const char ("%s") into a float. // fscanf takes the address of the var it should store the result in. fscanf (input_file, "%s",en); // you are comparing a string and a float - strcmp takes two const char*s if(strcmp (line, en) == 0){ // pFile[k] is not defined, and you forgot one float, // at least according to your syntax descr: // trackin 0.0 0.0 0.0 0.0 // weight 0.0 0.0 fscanf (pFile[k],"%*f %*f %f", &f4); en_in = f4; } // see above fscanf (input_file, "%s",wei); // see above if(strcmp (line2, wei) == 0){ // see above fscanf (pFile[k],"%*f %f ", &w2); w3 = en_in * w2; w3 = log10(w3); cout<< " EN_IN BEFORE = "<