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 48
|
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
int n;
int line_number = 0;
ofstream outfile;
ifstream infile;
string filename;
cout << "enter the input file name: ";
cin >> filename;
cout<< "input the line number ";
cin>> n;
infile.open(filename.c_str());
if (infile.fail())
{ cout << "Error opening " << filename << "\n";
return 1;
}
else {
int line_number = 0;
while (!infile.eof())
{
line_number++;
string line;
infile.getline(line);
if (line_number==n){
int size = line.length();
while (size >= 0)
{
cout << line[size];
size--;
}
}
}
}
infile.close();
system ("pause");
return 0;
}
|
thats what I did, but I am not sure if it should look like that because line_number is still the number of the line, not the actual line.
p.s.Now is the problem with the line corrected ?
int size = line.length();
It gives me the following errors :
[Warning] In function `int main()':
no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::getline(std::string&)'
candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]