Having trouble with getting int out of .txt

Hello, I need to get int out of .txt file, what I did, didnt work. I need to get 3 int out of it:
3
5 30
3 11
4 25

D-3
M-5,3 and 4
S-30,11 and 25.
Heres what I wrote :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream file("Text.txt");
int d,m,s;
if(file.is_open())
{
    while(file >> d >> m >> s)
    {
        cout << d << " " << m << " " << s << endl;
    }
    file.close();
}
else cout << "Has not opened" << endl;
return 0;
}
#include <iostream>
// #include <iomanip>
// #include <cmath>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream file("Text.txt");
int d,m,s;
if( file.is_open())
{
while (!file.eof()){
file >> d >> m >> s;
cout << d << " " << m << " " << s << endl;
}
file.close();
}
else cout << "Has not opened" << endl;
return 0;
}
It doesnt seem to print out numbers correcly.
Last edited on
Ok, I have managed to get a little closer now. Is there a way to what I marked with /* make shorter ? Thank you.

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
33
34
35
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int d,m,s,b=0,l=0;
ifstream file("Text.txt");
if(file.is_open())
{
    cout << "File has opened." << endl;
/*file >> d;
cout << d << endl;
file >> m >> s;
cout << m << " " << s << endl;
b+=m;
l+=s;
file >> m >> s;
cout << m << " " << s << endl;
b+=m;
l+=s;
file >> m >> s;
cout << m << " " << s << endl;
b+=m;
l+=s;
file.close();*/
}
else cout << "File has not opened." << endl;
cout << "D=" << d << endl;
cout << "B=" << b << endl;
cout << "L=" << l << endl;
return 0;
}
Wait a minute... You know how to open, close, and read file, but you don't know how to use a loop?

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
// #include <iomanip>  not needed
// #include <cmath>    not needed
#include <fstream>
// #include <string>    not needed
using namespace std;
int main()
{

int d,m,s,b=0,l=0;

ifstream file("Text.txt");

if(file.is_open())
{
cout << "File has opened." << endl;}
else  {cout << "File has not opened." << endl;   // -------------------- move this here
return 0; // -------------------------------------------- add this to end program if file is not opened
}

file >> d;
cout << d << endl;

 // --------------------------------------- Declare a loop
file >> m >> s;
cout << m << " " << s << endl;
b+=m;
l+=s;
// ------------------------------------------  end the loop

/*  Eliminate this
file >> m >> s;
cout << m << " " << s << endl;
b+=m;
l+=s;
file >> m >> s;
cout << m << " " << s << endl;
b+=m;
l+=s;
*/

cout << "D=" << d << endl;
cout << "B=" << b << endl;
cout << "L=" << l << endl;

return 0;
}



Something like that should be okay
Thank you pearlyman. Theres one more question, how can I read files till end of the line ?
I have this already:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double d,a,b,c;
string line;
ifstream file("Text2.txt");
if(file.is_open())
{
    file >> d;
    cout << d;
    ofstream file2("Result.txt");
    while(getline(file,line))
    {
       cout << line << endl;
    }
}
return 0;
}


I need to get two integers out of 'line', 1st integer as 1st number from a new line, and 2nd integer as numbers after the 1st number. I have already got first lines first number, dont know how to get others because of different lenght.
My text numbers:
4
3 24.5 59.0 71.7
2 45 66.2
2 34.1 45.1
4 45.2 56.4 45.1 56.2
Last edited on
Topic archived. No new replies allowed.