The getline() function also receives open characters, but when I execute the code, it just comes out connected I don't know why In test.txt, test1 test2 test3 It's in here
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream file;
file.open("test.txt");
if (file.is_open())
{
while (!file.eof())
{
string arr;
getline(file, arr);
cout << arr;
}
}
return 0;
}
The output is test1test2test3
fileinputstream
The getline function does not accept open characters.
Instead, the getline function is used to receive a line containing a space (such as a blank or tap character).
In the case of sin, you do not receive a string containing a space, so you use it.
cout << arr;
The code above should be changed as below.
cout << arr << '\n';
Please also refer to the coding style below.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream ifs("test.txt");
if (ifs.is_open()) {
string line;
while (getline(ifs, line))
cout << line << '\n';
}
}
© 2024 OneMinuteCode. All rights reserved.