You can use these files to test your program, right-click to download them: TestFile5Lines.txt and TestFile20Lines.txt
I tried to do this first using TestFile20Lines.txt which looks like this:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// go to the end of the file and count 10 lines from the bottom
int LENGTH;
fstream nameFile;
char input(LENGTH - 10);
// open file
nameFile.open("TestFile20Lines.txt", ios::in);
if (!nameFile)
{
cout << "File open error!" << endl;
return 0;
}
// read file one line at a time
// display the last 10 lines in the file. if the file has less than 10 lines
// display the entire file and print a message "The Entire File Has Been Displayed".
nameFile.getline(input,LENGTH);
while (!nameFile.fail())
{
cout << input << endl;
nameFile.getline(input, LENGTH);
}
if (LENGTH < 10)
{
cout << "The Entire File Has Been Displayed" << endl;
}
// close the file
nameFile.close();
system("Pause");
return 0;
}
The errors I receive and need help resolving:
1>------ Build started: Project: TailOfFile, Configuration: Debug Win32 ------
1> TailOfFile.cpp
1>c:\users\kim\documents\visual studio 2010\projects\tailoffile\tailoffile\tailoffile.cpp(39): error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'char' to 'char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\kim\documents\visual studio 2010\projects\tailoffile\tailoffile\tailoffile.cpp(43): error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'char' to 'char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

New Topic/Question
Reply




MultiQuote



|