ASCII file analysis problem

Hi rooters:

I’m building a ROOT-based SAA and I have a problem that (maybe) comes from my lack of knowledge about C++ :blush:

This is the core of the api (a version of basic.C):


#include “Riostream.h”
#include “TApplication.h”
#include “TH1D.h”
#include

using namespace std;

int main(int argc, char **argv)
{
TApplication *myApp = new TApplication(“myApp”,&argc,argv);

ifstream in;
Double_t x[1000];
char line[20];

for ( int i = 0; i<100; i++) x[i]=0;
in.open(“dummy.txt”);

Int_t nlines = 0;
TFile *f = new TFile(“basic.root”,“RECREATE”);

while (!in.eof()) {
in.getline(line,255); // read a line of the file
istrstream st(line);
st >> x[nlines] ;
if (!in.good()) break;
if ( nlines < 3 ) {
//x = in.get(); //it doesn work properly !
cout << " x = " << x[nlines] << endl;
nlines++;
}
}

//it prints the number of lines
printf(" found %d lines \n",nlines);

// close the file
in.close();
f->Write();
myApp->Run();
return 0;
}

The first thing that I want to do is to store the data from a file like this (file “dummy.txt”)

Hello: 10
Value: 5
1 2 3 4 5 6 7 8

that is, 2 labelled numbers and an array. In an future I will read this “x” times. I had tried with “in.ignore(6)” for example, to avoid the chain, but I obtain only crap (maybe hexdec values of the letters ?)

My ROOT version is 4.03/02 16 February 2005

Thanks for helping me !

:smiley:

You never actually make it clear what your specific problem is,
but there are definitely mistakes in your code, for example the
getline() call can cause a memory overwrite.

Following is some example code which does the i/o part of your
question. Some of the error handling code is commented out
because it provokes a bug in the cint bytecode compiler, but
the commented out code works fine if you compile using ACLiC.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void problem()
{
  int i;
  string s;
  vector<int> v;
  ifstream infile;

  // handle i/o errors by throwing an exception
  infile.exceptions(ios_base::badbit | ios_base::failbit);
  infile.open("problem.txt");

  // get label and number
  infile >> s >> i;
  cout << s << " " << i << endl;

  // get label and number
  infile >> s >> i;
  cout << s << " " << i << endl;

  //
  // now get the array of numbers
  //

  // Note:  We have to take failbit out
  //            of the exception handling
  //            because failbit gets set when
  //            we reach eof, so we must test
  //            for that condition by hand.
  infile.exceptions(ios_base::badbit);

  // We must attempt a read before
  // checking for eof, it will not be set
  // until we try to read.
  infile >> i;

  while (!infile.eof()) {

    // Note:  At this point we may have failbit
    //           set because the next thing in the
    //           file was not a number.

    // This next line is commented out because
    // of a bug in the cint bytecode compiler.
    // In compiled code it should be used.
    //
    //if (infile.fail()) throw "integer conversion error";

    // remember the number we just read
    v.push_back(i);

    // try the next read, eofbit will be set when
    // we reach the end of the file
    infile >> i;
  }
  infile.close();

  // print out the array
  cout << "array:";
  for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter) {
    cout << " " << *iter;
  }
  cout << endl;
}

Ok, I’ll test it and I will post here my results!
I have no experience handling exceptions, so, I’ll check this part especially.
Thanks !

8)

[quote=“Physlock”]Ok, I’ll test it and I will post here my results!
I have no experience handling exceptions, so, I’ll check this part especially.
Thanks !

8)[/quote]
The exception handling is just a way to exit quickly when an
error occurs without having to check after each i/o statement.
It is not vital to the actual function of the example, but it is a
common programming mistake to not check error codes after
doing an i/o, so I always attempt to include at least minimal
error handling in an example.

Hi !

Finally I had finished my SAA but I had used the “fscan” stuff from the STL “stdio”. First I open in the “main” the file with:

FILE *file = fopen(“Data.txt”, “r”);

Then I call a function "reading(file, more arguments) in a while (!feof(file)) { } where I use the

fscanf(file, “Hola:%d”, &holavalue); //flag 4 pointer (int) decimal

and after that I close the file with

hfile.Close();

A lot of thanks anyway !!!

8)