K
Write it either.string data;
while ( getline( fin, data ) )
{
cout << data << endl;
}
orstring data;
while ( getline( fin, data ) )
{
cout << data << '\n';
}
orstring data;
while ( fin >> data )
{
cout << data << std::endl;
}
orstring data;
while ( fin >> data )
{
cout << data << '\n';
}
In the last two cases, you can actually use some arithmetic type that matches the input values.For example,int data;
while ( fin >> data )
{
cout << data << '\n';
}
You can use algorithm. std::copyin the title <algorithm>instead of the cycle while♪For example#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
//...
std::ifstream fin( "/Users/Margarita/XCode/Full_As1/user_file.txt" );
std::copy( std::istream_iterator<int>( fin ),
std::istream_iterator<int>(),
std::ostream_iterator<int>( std::cout, "\n" ) );
You mean your cycle isn't really right. while (!fin.eof())
{
getline (fin,data);
cout<< data
}
The problem is that the end of the file may occur at the time the proposal is implemented getline (fin,data);
In this case, in the line data Nothing will be introduced. Still, you're putting her on the console with the following sentence. cout<< data
The effect may be that the last recording in the file will be out twice.