C_str() does not work?

Hello, I experience a strange problem. I think the fix is obvious but I do not manage to find it
I try to get the content of the file I attached with the following code

[code]#include
#include
#include
#include
#include

using namespace std;

void test() {
ifstream ifile(“test.txt”);
if(!ifile) {
cout << “Problem with test.txt” << endl;
return;
}

string line;
stringstream ss;
string tmp1, tmp2, tmp3, tmp4;

getline(ifile,line);
while(getline(ifile,line)) {
// 4;21/07/2015;15:20:14;111.680779
ss << line;
getline(ss,tmp1,’;’);
getline(ss,tmp2,’;’);
getline(ss,tmp3,’;’);
getline(ss,tmp4,’;’);

cout <<  tmp1 << " <" << tmp1.c_str() << ">, " << atoi(tmp1.c_str()) << endl;

}
}[/code]

The problem is I get this output

[quote]root [0] .x test.C++
Info in TUnixSystem::ACLiC: creating shared library /home/pamputt/dev/./test_C.so
4 <>, 0
4 <>, 0
4 <>, 0
4 <>, 0
4 <>, 0
4 <>, 0
4 <>, 0[/quote]
That means that the c_str() method returns an empty char string and I do not understand why. Sometyhing worng with my txt file? Any idea?

PS : I get this result on ROOT 6.04/06 on 64-bits linux
test.txt (432 Bytes)

The getline, to read the line in the file, is already wrong…

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>

using namespace std;

void test() {
  ifstream ifile("test.txt");
  if(!ifile) {
    cout << "Problem with test.txt" << endl;
    return;
  }

  string line;
  stringstream ss;
  string tmp1, tmp2, tmp3, tmp4;

  getline(ifile,line);
  while(getline(ifile,line)) {
    // 4;21/07/2015;15:20:14;111.680779
    printf("%s\n",line.c_str());
    ss << line;
    getline(ss,tmp1,';');
    getline(ss,tmp2,';');
    getline(ss,tmp3,';');
    getline(ss,tmp4,';');

    //cout <<  tmp1 << " <" << tmp1.c_str() << ">, " << atoi(tmp1.c_str()) << endl;
  }
}

gives:

Processing test.C...







root [1] 

“line” is empty … I guess there is example in some C forums showing how to use it … that’s not a ROOT question per se

Indeed, written like this it is not a ROOT question.
Initially I wanted to read my txt file using the ReadFile method of the TTree class.
As it did not work, I tried to find where the problem could come from and I arrive a that point.
I will try to find the fix on a C forum.

If you change the txt file as follow (# in 1st line)

# Capteur_ID;Date;Heure;Valeur
4;21/07/2015;15:20:14;111.680779
5;21/07/2015;15:20:14;111.875061
6;21/07/2015;15:20:14;111.750183
7;21/07/2015;15:20:14;4000.4
8;21/07/2015;15:20:14;3500.5
9;21/07/2015;15:20:14;4500.1

then the following macro:

void test() {
  TTree t;
  t.ReadFile("test.txt","CapteurID/I:Date/C:Heure/C:Valeur/F",';');
  t.Scan();
}

gives:

root [0] .x test.C
************************************************************
*    Row   * CapteurID *      Date *     Heure *    Valeur *
************************************************************
*        0 *         4 * 21/07/201 *  15:20:14 * 111.68077 *
*        1 *         5 * 21/07/201 *  15:20:14 * 111.87506 *
*        2 *         6 * 21/07/201 *  15:20:14 * 111.75018 *
*        3 *         7 * 21/07/201 *  15:20:14 * 4000.3999 *
*        4 *         8 * 21/07/201 *  15:20:14 *    3500.5 *
*        5 *         9 * 21/07/201 *  15:20:14 * 4500.1001 *
************************************************************
root [1] 

Fix your txt file to be ascii text file and the problem will magically disappear (ah, and move ss declaration inside the loop).

Indeed, the problem came from the first line of the text file. After fixing this issue, everything works fine. Thank you.

Yes it had some unknown characters. You can go back to your first idea using ReadFile().