#include #include #include #include using namespace std; const int MAX_CHARS_PER_LINE = 1000; const int MAX_TOKENS_PER_LINE = 10; const char* DELIMITER = "\t"; bool debug = true; const char* infname = "data.txt"; // main routine void ReadData() { // create a file-reading object //------------------------------ ifstream infile; infile.open(infname); // open a file if (infile.fail()){ cerr << "\nData file could not be opened.\n"; continue;// exit if file not found } // parse the acm file by reading line by line while (!infile.eof()) { // read an entire line into memory char buf[MAX_CHARS_PER_LINE]; infile.getline(buf, MAX_CHARS_PER_LINE); // parse the line into tab-delimited tokens // array to store memory addresses of the tokens in buf char* token[MAX_TOKENS_PER_LINE] = {0}; // initialize to 0 char* pEnd;// used with strtod(s,p) as pointer to end character // atof() did not really work very well, as we'd better // keep double precision // parse the line token[0] = strtok(buf, DELIMITER); // first token // now find what we need and do stuff if (token[0]) // zero if line is blank { if(debug) cerr << atoi(token[0]) << " "; for (Int_t n = 1; n < MAX_TOKENS_PER_LINE; n++) { token[n] = strtok(NULL, DELIMITER); // subsequent tokens if (!token[n]) break; // no more tokens //val = atof(number.c_str()); val = strtod(token[n], &pEnd); cerr << "tok[n]:" << token[n] << ", val=" << val << "\t"; } cerr << "\n"; } if (!token[0]) break; // no more stuff at all } // end while() loop infile.close(); }// end ReadData()