New lines with fstream

Hi
I have to analyze and elaborate datas from particle energy measurements. I have a file txt like this

1
2
1 126 17 20 23 32 39 55 68 81 98 111 129 141 151 164 170 182 180 183 187 187 181 186 178    174 175 172 163 159 155 147 140 138 128 122 105 100 99 87 80 66 59 55 48 42 40 33 31 25 25 22 19 19 18 15 15 16 15 16 15 13 13 14 14 14 14 13 13 14 14 13 15 15 14 15 14 14 13 12 13 14 13 14 14 14 13 14 14 13 12 13 13 13 13 14 13 13 13 13 14 14 14 13 13 14 14 14 12 12 13 12 13 11 12 13 12 13 14 12 12 12 11 13 12 13 13 15
2 124 20 34 9 34 51 50 61 76 71 69 95 99 108 103 101 95 89 117 98 107 76 86 66 97 97 80 17 54 45 44 39 40 18 13 29 20 16 15 15 16 14 15 16 16 15 15 14 13 12 14 14 14 12 14 13 12 15 12 12 14 14 11 12 12 14 12 12 13 12 12 13 12 12 12 13 12 13 13 13 12 14 12 12 13 10 12 13 12 13 13 13 12 12 12 13 12 12 13 12 10 15 13 13 12 14 12 12 14 12 12 13 13 13 12 12 12 11 11 13 14 12 12 12 13

The firts two numbers are the Particle ID and its Energy, the rest of numbers are the loss of energy(different in number for every particle).
I have to elaborate 1000 events like this and to create a function able to read every line and to count the number of the loss energy measurement.
I use ifstream to read a file text, but my problem is: my program how to understand that it's passing to the new line? I have to divided every events in Arrays of different length, so i have to count each data for every particle and then create the array.
The instruction EOF it's not good...
thanks!
getline() for string. That reads one whole line into a std::string. Initialize an istringstream from that string. Then read from the new stream.
Thanks! It's what i was looking for!
This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;        
 int read(ifstream& file) {
     string input;
     int ID, Energy, Loss, n=0;
     getline(file,input);
     istringstream iss (input);
     iss >> ID >> Energy;
     cout << "ID: " << ID << "\tEnergy: " << Energy << "\t Loss: ";
     while (iss>>Loss) {
         cout << Loss << "(" << n <<") ";
         n++;
     }
     return n;
            

 }

int main(int argc, const char * argv[])
{
    ifstream file ("events");
    int Number=0;
    while (file.good())
    {
        cout << "Lettura File: " << file.good() << endl;
        cout << "Number of Loss Energy Measure: " << (Number= read(file)) << endl;
    }
    return 0;
}

And this is my output:
1
2
3
4
5
6
Lettura File: 1
ID: 1	Energy: 126	 Loss: 17(0) 20(1) 23(2) 32(3) 39(4) 55(5) 68(6) 81(7) 98(8) 111(9) 129(10) 141(11) 151(12) 164(13) 170(14) 182(15) 180(16) 183(17) 187(18) 187(19) 181(20) 186(21) 178(22) 174(23) 175(24) 172(25) 163(26) 159(27) 155(28) 147(29) 140(30) 138(31) 128(32) 122(33) 105(34) 100(35) 99(36) 87(37) 80(38) 66(39) 59(40) 55(41) 48(42) 42(43) 40(44) 33(45) 31(46) 25(47) 25(48) 22(49) 19(50) 19(51) 18(52) 15(53) 15(54) 16(55) 15(56) 16(57) 15(58) 13(59) 13(60) 14(61) 14(62) 14(63) 14(64) 13(65) 13(66) 14(67) 14(68) 13(69) 15(70) 15(71) 14(72) 15(73) 14(74) 14(75) 13(76) 12(77) 13(78) 14(79) 13(80) 14(81) 14(82) 14(83) 13(84) 14(85) 14(86) 13(87) 12(88) 13(89) 13(90) 13(91) 13(92) 14(93) 13(94) 13(95) 13(96) 13(97) 14(98) 14(99) 14(100) 13(101) 13(102) 14(103) 14(104) 14(105) 12(106) 12(107) 13(108) 12(109) 13(110) 11(111) 12(112) 13(113) 12(114) 13(115) 14(116) 12(117) 12(118) 12(119) 11(120) 13(121) 12(122) 13(123) 13(124) 15(125) Number of Loss Energy Measure: 126
Lettura File: 1
ID: 2	Energy: 124	 Loss: 20(0) 34(1) 9(2) 34(3) 51(4) 50(5) 61(6) 76(7) 71(8) 69(9) 95(10) 99(11) 108(12) 103(13) 101(14) 95(15) 89(16) 117(17) 98(18) 107(19) 76(20) 86(21) 66(22) 97(23) 97(24) 80(25) 17(26) 54(27) 45(28) 44(29) 39(30) 40(31) 18(32) 13(33) 29(34) 20(35) 16(36) 15(37) 15(38) 16(39) 14(40) 15(41) 16(42) 16(43) 15(44) 15(45) 14(46) 13(47) 12(48) 14(49) 14(50) 14(51) 12(52) 14(53) 13(54) 12(55) 15(56) 12(57) 12(58) 14(59) 14(60) 11(61) 12(62) 12(63) 14(64) 12(65) 12(66) 13(67) 12(68) 12(69) 13(70) 12(71) 12(72) 12(73) 13(74) 12(75) 13(76) 13(77) 13(78) 12(79) 14(80) 12(81) 12(82) 13(83) 10(84) 12(85) 13(86) 12(87) 13(88) 13(89) 13(90) 12(91) 12(92) 12(93) 13(94) 12(95) 12(96) 13(97) 12(98) 10(99) 15(100) 13(101) 13(102) 12(103) 14(104) 12(105) 12(106) 14(107) 12(108) 12(109) 13(110) 13(111) 13(112) 12(113) 12(114) 12(115) 11(116) 11(117) 13(118) 14(119) 12(120) 12(121) 12(122) 13(123) Number of Loss Energy Measure: 124
Lettura File: 1
ID: 0	Energy: 0	 Loss: Number of Loss Energy Measure: 0


My program reads also the last empty line... how can i ignore it?
You create iss without checking whether you actually got anything with getline.
How?
Oh, was not answering "how?", but "why?".

IF input is not empty
THEN create the iss and so forth.
Topic archived. No new replies allowed.